3 * $Id: mpx.c,v 1.10 2000/10/08 12:06:12 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.10 2000/10/08 12:06:12 mdw
34 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
36 * Revision 1.9 2000/06/26 07:52:50 mdw
37 * Portability fix for the bug fix.
39 * Revision 1.8 2000/06/25 12:59:02 mdw
40 * (mpx_udiv): Fix bug in quotient digit estimation.
42 * Revision 1.7 1999/12/22 15:49:07 mdw
43 * New function for division by a small integer.
45 * Revision 1.6 1999/11/20 22:43:44 mdw
46 * Integrate testing for MPX routines.
48 * Revision 1.5 1999/11/20 22:23:27 mdw
49 * Add function versions of some low-level macros with wider use.
51 * Revision 1.4 1999/11/17 18:04:09 mdw
52 * Add two's-complement functionality. Improve mpx_udiv a little by
53 * performing the multiplication of the divisor by q with the subtraction
56 * Revision 1.3 1999/11/13 01:57:31 mdw
57 * Remove stray debugging code.
59 * Revision 1.2 1999/11/13 01:50:59 mdw
60 * Multiprecision routines finished and tested.
62 * Revision 1.1 1999/09/03 08:41:12 mdw
67 /*----- Header files ------------------------------------------------------*/
74 #include <mLib/bits.h>
79 /*----- Loading and storing -----------------------------------------------*/
81 /* --- @mpx_storel@ --- *
83 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
84 * @void *pp@ = pointer to octet array
85 * @size_t sz@ = size of octet array
89 * Use: Stores an MP in an octet array, least significant octet
90 * first. High-end octets are silently discarded if there
91 * isn't enough space for them.
94 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
97 octet
*p
= pp
, *q
= p
+ sz
;
107 *p
++ = U8(w
| n
<< bits
);
109 bits
+= MPW_BITS
- 8;
119 /* --- @mpx_loadl@ --- *
121 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
122 * @const void *pp@ = pointer to octet array
123 * @size_t sz@ = size of octet array
127 * Use: Loads an MP in an octet array, least significant octet
128 * first. High-end octets are ignored if there isn't enough
132 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
136 const octet
*p
= pp
, *q
= p
+ sz
;
145 if (bits
>= MPW_BITS
) {
147 w
= n
>> (MPW_BITS
- bits
+ 8);
157 /* --- @mpx_storeb@ --- *
159 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
160 * @void *pp@ = pointer to octet array
161 * @size_t sz@ = size of octet array
165 * Use: Stores an MP in an octet array, most significant octet
166 * first. High-end octets are silently discarded if there
167 * isn't enough space for them.
170 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
173 octet
*p
= pp
, *q
= p
+ sz
;
183 *--q
= U8(w
| n
<< bits
);
185 bits
+= MPW_BITS
- 8;
195 /* --- @mpx_loadb@ --- *
197 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
198 * @const void *pp@ = pointer to octet array
199 * @size_t sz@ = size of octet array
203 * Use: Loads an MP in an octet array, most significant octet
204 * first. High-end octets are ignored if there isn't enough
208 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
212 const octet
*p
= pp
, *q
= p
+ sz
;
221 if (bits
>= MPW_BITS
) {
223 w
= n
>> (MPW_BITS
- bits
+ 8);
233 /*----- Logical shifting --------------------------------------------------*/
235 /* --- @mpx_lsl@ --- *
237 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
238 * @const mpw *av, *avl@ = source vector base and limit
239 * @size_t n@ = number of bit positions to shift by
243 * Use: Performs a logical shift left operation on an integer.
246 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
251 /* --- Trivial special case --- */
254 MPX_COPY(dv
, dvl
, av
, avl
);
256 /* --- Single bit shifting --- */
265 *dv
++ = MPW((t
<< 1) | w
);
266 w
= t
>> (MPW_BITS
- 1);
275 /* --- Break out word and bit shifts for more sophisticated work --- */
280 /* --- Handle a shift by a multiple of the word size --- */
283 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
284 memset(dv
, 0, MPWS(nw
));
287 /* --- And finally the difficult case --- *
289 * This is a little convoluted, because I have to start from the end and
290 * work backwards to avoid overwriting the source, if they're both the same
296 size_t nr
= MPW_BITS
- nb
;
297 size_t dvn
= dvl
- dv
;
298 size_t avn
= avl
- av
;
305 if (dvn
> avn
+ nw
) {
306 size_t off
= avn
+ nw
+ 1;
307 MPX_ZERO(dv
+ off
, dvl
);
317 *--dvl
= (t
>> nr
) | w
;
328 /* --- @mpx_lsr@ --- *
330 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
331 * @const mpw *av, *avl@ = source vector base and limit
332 * @size_t n@ = number of bit positions to shift by
336 * Use: Performs a logical shift right operation on an integer.
339 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
344 /* --- Trivial special case --- */
347 MPX_COPY(dv
, dvl
, av
, avl
);
349 /* --- Single bit shifting --- */
358 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
368 /* --- Break out word and bit shifts for more sophisticated work --- */
373 /* --- Handle a shift by a multiple of the word size --- */
376 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
378 /* --- And finally the difficult case --- */
382 size_t nr
= MPW_BITS
- nb
;
391 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
395 *dv
++ = MPW(w
>> nb
);
403 /*----- Unsigned arithmetic -----------------------------------------------*/
405 /* --- @mpx_2c@ --- *
407 * Arguments: @mpw *dv, *dvl@ = destination vector
408 * @const mpw *v, *vl@ = source vector
412 * Use: Calculates the two's complement of @v@.
415 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
418 while (dv
< dvl
&& v
< vl
)
419 *dv
++ = c
= MPW(~*v
++);
426 MPX_UADDN(dv
, dvl
, 1);
429 /* --- @mpx_ueq@ --- *
431 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
432 * @const mpw *bv, *bvl@ = second argument vector base and limit
434 * Returns: Nonzero if the two vectors are equal.
436 * Use: Performs an unsigned integer test for equality.
439 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
443 if (avl
- av
!= bvl
- bv
)
452 /* --- @mpx_ucmp@ --- *
454 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
455 * @const mpw *bv, *bvl@ = second argument vector base and limit
457 * Returns: Less than, equal to, or greater than zero depending on
458 * whether @a@ is less than, equal to or greater than @b@,
461 * Use: Performs an unsigned integer comparison.
464 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
469 if (avl
- av
> bvl
- bv
)
471 else if (avl
- av
< bvl
- bv
)
473 else while (avl
> av
) {
474 mpw a
= *--avl
, b
= *--bvl
;
483 /* --- @mpx_uadd@ --- *
485 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
486 * @const mpw *av, *avl@ = first addend vector base and limit
487 * @const mpw *bv, *bvl@ = second addend vector base and limit
491 * Use: Performs unsigned integer addition. If the result overflows
492 * the destination vector, high-order bits are discarded. This
493 * means that two's complement addition happens more or less for
494 * free, although that's more a side-effect than anything else.
495 * The result vector may be equal to either or both source
496 * vectors, but may not otherwise overlap them.
499 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
500 const mpw
*bv
, const mpw
*bvl
)
504 while (av
< avl
|| bv
< bvl
) {
509 a
= (av
< avl
) ?
*av
++ : 0;
510 b
= (bv
< bvl
) ?
*bv
++ : 0;
511 x
= (mpd
)a
+ (mpd
)b
+ c
;
521 /* --- @mpx_uaddn@ --- *
523 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
524 * @mpw n@ = other addend
528 * Use: Adds a small integer to a multiprecision number.
531 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
533 /* --- @mpx_usub@ --- *
535 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
536 * @const mpw *av, *avl@ = first argument vector base and limit
537 * @const mpw *bv, *bvl@ = second argument vector base and limit
541 * Use: Performs unsigned integer subtraction. If the result
542 * overflows the destination vector, high-order bits are
543 * discarded. This means that two's complement subtraction
544 * happens more or less for free, althuogh that's more a side-
545 * effect than anything else. The result vector may be equal to
546 * either or both source vectors, but may not otherwise overlap
550 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
551 const mpw
*bv
, const mpw
*bvl
)
555 while (av
< avl
|| bv
< bvl
) {
560 a
= (av
< avl
) ?
*av
++ : 0;
561 b
= (bv
< bvl
) ?
*bv
++ : 0;
562 x
= (mpd
)a
- (mpd
)b
- c
;
575 /* --- @mpx_usubn@ --- *
577 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
582 * Use: Subtracts a small integer from a multiprecision number.
585 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
587 /* --- @mpx_umul@ --- *
589 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
590 * @const mpw *av, *avl@ = multiplicand vector base and limit
591 * @const mpw *bv, *bvl@ = multiplier vector base and limit
595 * Use: Performs unsigned integer multiplication. If the result
596 * overflows the desination vector, high-order bits are
597 * discarded. The result vector may not overlap the argument
598 * vectors in any way.
601 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
602 const mpw
*bv
, const mpw
*bvl
)
604 /* --- This is probably worthwhile on a multiply --- */
609 /* --- Deal with a multiply by zero --- */
616 /* --- Do the initial multiply and initialize the accumulator --- */
618 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
620 /* --- Do the remaining multiply/accumulates --- */
622 while (dv
< dvl
&& bv
< bvl
) {
632 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
636 MPX_UADDN(dvv
, dvl
, c
);
641 /* --- @mpx_umuln@ --- *
643 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
644 * @const mpw *av, *avl@ = multiplicand vector base and limit
645 * @mpw m@ = multiplier
649 * Use: Multiplies a multiprecision integer by a single-word value.
650 * The destination and source may be equal. The destination
651 * is completely cleared after use.
654 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
656 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
659 /* --- @mpx_umlan@ --- *
661 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
662 * @const mpw *av, *avl@ = multiplicand vector base and limit
663 * @mpw m@ = multiplier
667 * Use: Multiplies a multiprecision integer by a single-word value
668 * and adds the result to an accumulator.
671 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
673 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
676 /* --- @mpx_usqr@ --- *
678 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
679 * @const mpw *av, *av@ = source vector base and limit
683 * Use: Performs unsigned integer squaring. The result vector must
684 * not overlap the source vector in any way.
687 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
691 /* --- Main loop --- */
699 /* --- Stop if I've run out of destination --- */
704 /* --- Work out the square at this point in the proceedings --- */
707 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
709 c
= MPW(x
>> MPW_BITS
);
712 /* --- Now fix up the rest of the vector upwards --- */
715 while (dvv
< dvl
&& avv
< avl
) {
716 mpd x
= (mpd
)a
* (mpd
)*avv
++;
717 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
718 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
721 while (dvv
< dvl
&& c
) {
727 /* --- Get ready for the next round --- */
734 /* --- @mpx_udiv@ --- *
736 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
737 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
738 * @const mpw *dv, *dvl@ = divisor vector base and limit
739 * @mpw *sv, *svl@ = scratch workspace
743 * Use: Performs unsigned integer division. If the result overflows
744 * the quotient vector, high-order bits are discarded. (Clearly
745 * the remainder vector can't overflow.) The various vectors
746 * may not overlap in any way. Yes, I know it's a bit odd
747 * requiring the dividend to be in the result position but it
748 * does make some sense really. The remainder must have
749 * headroom for at least two extra words. The scratch space
750 * must be at least one word larger than the divisor.
753 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
754 const mpw
*dv
, const mpw
*dvl
,
761 /* --- Initialize the quotient --- */
765 /* --- Perform some sanity checks --- */
768 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
770 /* --- Normalize the divisor --- *
772 * The algorithm requires that the divisor be at least two digits long.
773 * This is easy to fix.
780 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
781 if (d
< (MPW_MAX
>> b
)) {
790 /* --- Normalize the dividend/remainder to match --- */
793 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
794 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
804 /* --- Work out the relative scales --- */
807 size_t rvn
= rvl
- rv
;
808 size_t dvn
= dvl
- dv
;
810 /* --- If the divisor is clearly larger, notice this --- */
813 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
820 /* --- Calculate the most significant quotient digit --- *
822 * Because the divisor has its top bit set, this can only happen once. The
823 * pointer arithmetic is a little contorted, to make sure that the
824 * behaviour is defined.
827 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
828 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
829 if (qvl
- qv
> scale
)
833 /* --- Now for the main loop --- */
842 /* --- Get an estimate for the next quotient digit --- */
849 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
855 /* --- Refine the estimate --- */
859 mpd yy
= (mpd
)dd
* q
;
863 yh
+= yy
>> MPW_BITS
;
866 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
875 /* --- Remove a chunk from the dividend --- */
882 /* --- Calculate the size of the chunk --- *
884 * This does the whole job of calculating @r >> scale - qd@.
887 for (svv
= rv
+ scale
, dvv
= dv
;
888 dvv
< dvl
&& svv
< rvl
;
890 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
892 x
= (mpd
)*svv
- MPW(x
) - sc
;
901 mpd x
= (mpd
)*svv
- mc
- sc
;
911 /* --- Fix if the quotient was too large --- *
913 * This doesn't seem to happen very often.
916 if (rvl
[-1] > MPW_MAX
/ 2) {
917 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
922 /* --- Done for another iteration --- */
924 if (qvl
- qv
> scale
)
931 /* --- Now fiddle with unnormalizing and things --- */
933 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
936 /* --- @mpx_udivn@ --- *
938 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
940 * @const mpw *rv, *rvl@ = dividend
941 * @mpw d@ = single-precision divisor
943 * Returns: Remainder after divison.
945 * Use: Performs a single-precision division operation.
948 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
951 size_t ql
= qvl
- qv
;
957 r
= (r
<< MPW_BITS
) | rv
[i
];
965 /*----- Test rig ----------------------------------------------------------*/
969 #include <mLib/alloc.h>
970 #include <mLib/dstr.h>
971 #include <mLib/quis.h>
972 #include <mLib/testrig.h>
976 #define ALLOC(v, vl, sz) do { \
978 mpw *_vv = xmalloc(MPWS(_sz)); \
979 mpw *_vvl = _vv + _sz; \
984 #define LOAD(v, vl, d) do { \
985 const dstr *_d = (d); \
987 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
988 mpx_loadb(_v, _vl, _d->buf, _d->len); \
993 #define MAX(x, y) ((x) > (y) ? (x) : (y))
995 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1000 fprintf(stderr
, " %02x", *p
++);
1001 fputc('\n', stderr
);
1004 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1009 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1010 fputc('\n', stderr
);
1013 static int chkscan(const mpw
*v
, const mpw
*vl
,
1014 const void *pp
, size_t sz
, int step
)
1017 const octet
*p
= pp
;
1021 mpscan_initx(&mps
, v
, vl
);
1026 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1027 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1029 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1030 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1042 static int loadstore(dstr
*v
)
1045 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1049 dstr_ensure(&d
, v
->len
);
1050 m
= xmalloc(MPWS(sz
));
1052 for (diff
= 0; diff
< sz
; diff
+= 5) {
1057 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1058 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1060 MPX_OCTETS(oct
, m
, ml
);
1061 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1062 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1063 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1067 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1068 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1070 MPX_OCTETS(oct
, m
, ml
);
1071 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1072 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1073 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1079 dumpbits("input data", v
->buf
, v
->len
);
1086 static int lsl(dstr
*v
)
1089 int n
= *(int *)v
[1].buf
;
1096 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1098 mpx_lsl(d
, dl
, a
, al
, n
);
1099 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1100 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1101 dumpmp(" a", a
, al
);
1102 dumpmp("expected", c
, cl
);
1103 dumpmp(" result", d
, dl
);
1107 free(a
); free(c
); free(d
);
1111 static int lsr(dstr
*v
)
1114 int n
= *(int *)v
[1].buf
;
1121 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1123 mpx_lsr(d
, dl
, a
, al
, n
);
1124 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1125 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1126 dumpmp(" a", a
, al
);
1127 dumpmp("expected", c
, cl
);
1128 dumpmp(" result", d
, dl
);
1132 free(a
); free(c
); free(d
);
1136 static int uadd(dstr
*v
)
1147 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1149 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1150 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1151 fprintf(stderr
, "\n*** uadd failed\n");
1152 dumpmp(" a", a
, al
);
1153 dumpmp(" b", b
, bl
);
1154 dumpmp("expected", c
, cl
);
1155 dumpmp(" result", d
, dl
);
1159 free(a
); free(b
); free(c
); free(d
);
1163 static int usub(dstr
*v
)
1174 ALLOC(d
, dl
, al
- a
);
1176 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1177 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1178 fprintf(stderr
, "\n*** usub failed\n");
1179 dumpmp(" a", a
, al
);
1180 dumpmp(" b", b
, bl
);
1181 dumpmp("expected", c
, cl
);
1182 dumpmp(" result", d
, dl
);
1186 free(a
); free(b
); free(c
); free(d
);
1190 static int umul(dstr
*v
)
1201 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1203 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1204 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1205 fprintf(stderr
, "\n*** umul failed\n");
1206 dumpmp(" a", a
, al
);
1207 dumpmp(" b", b
, bl
);
1208 dumpmp("expected", c
, cl
);
1209 dumpmp(" result", d
, dl
);
1213 free(a
); free(b
); free(c
); free(d
);
1217 static int usqr(dstr
*v
)
1226 ALLOC(d
, dl
, 2 * (al
- a
));
1228 mpx_usqr(d
, dl
, a
, al
);
1229 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1230 fprintf(stderr
, "\n*** usqr failed\n");
1231 dumpmp(" a", a
, al
);
1232 dumpmp("expected", c
, cl
);
1233 dumpmp(" result", d
, dl
);
1237 free(a
); free(c
); free(d
);
1241 static int udiv(dstr
*v
)
1251 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1255 ALLOC(qq
, qql
, al
- a
);
1256 ALLOC(s
, sl
, (bl
- b
) + 1);
1258 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1259 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1260 !mpx_ueq(a
, al
, r
, rl
)) {
1261 fprintf(stderr
, "\n*** udiv failed\n");
1262 dumpmp(" divisor", b
, bl
);
1263 dumpmp("expect r", r
, rl
);
1264 dumpmp("result r", a
, al
);
1265 dumpmp("expect q", q
, ql
);
1266 dumpmp("result q", qq
, qql
);
1270 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1274 static test_chunk defs
[] = {
1275 { "load-store", loadstore
, { &type_hex
, 0 } },
1276 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1277 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1278 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1279 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1280 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1281 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1282 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1286 int main(int argc
, char *argv
[])
1288 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1294 /*----- That's all, folks -------------------------------------------------*/