3 * $Id: mpx.c,v 1.8 2000/06/25 12:59:02 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.8 2000/06/25 12:59:02 mdw
34 * (mpx_udiv): Fix bug in quotient digit estimation.
36 * Revision 1.7 1999/12/22 15:49:07 mdw
37 * New function for division by a small integer.
39 * Revision 1.6 1999/11/20 22:43:44 mdw
40 * Integrate testing for MPX routines.
42 * Revision 1.5 1999/11/20 22:23:27 mdw
43 * Add function versions of some low-level macros with wider use.
45 * Revision 1.4 1999/11/17 18:04:09 mdw
46 * Add two's-complement functionality. Improve mpx_udiv a little by
47 * performing the multiplication of the divisor by q with the subtraction
50 * Revision 1.3 1999/11/13 01:57:31 mdw
51 * Remove stray debugging code.
53 * Revision 1.2 1999/11/13 01:50:59 mdw
54 * Multiprecision routines finished and tested.
56 * Revision 1.1 1999/09/03 08:41:12 mdw
61 /*----- Header files ------------------------------------------------------*/
68 #include <mLib/bits.h>
73 /*----- Loading and storing -----------------------------------------------*/
75 /* --- @mpx_storel@ --- *
77 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
78 * @void *pp@ = pointer to octet array
79 * @size_t sz@ = size of octet array
83 * Use: Stores an MP in an octet array, least significant octet
84 * first. High-end octets are silently discarded if there
85 * isn't enough space for them.
88 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
91 octet
*p
= pp
, *q
= p
+ sz
;
101 *p
++ = U8(w
| n
<< bits
);
103 bits
+= MPW_BITS
- 8;
113 /* --- @mpx_loadl@ --- *
115 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
116 * @const void *pp@ = pointer to octet array
117 * @size_t sz@ = size of octet array
121 * Use: Loads an MP in an octet array, least significant octet
122 * first. High-end octets are ignored if there isn't enough
126 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
130 const octet
*p
= pp
, *q
= p
+ sz
;
139 if (bits
>= MPW_BITS
) {
141 w
= n
>> (MPW_BITS
- bits
+ 8);
151 /* --- @mpx_storeb@ --- *
153 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
154 * @void *pp@ = pointer to octet array
155 * @size_t sz@ = size of octet array
159 * Use: Stores an MP in an octet array, most significant octet
160 * first. High-end octets are silently discarded if there
161 * isn't enough space for them.
164 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
167 octet
*p
= pp
, *q
= p
+ sz
;
177 *--q
= U8(w
| n
<< bits
);
179 bits
+= MPW_BITS
- 8;
189 /* --- @mpx_loadb@ --- *
191 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
192 * @const void *pp@ = pointer to octet array
193 * @size_t sz@ = size of octet array
197 * Use: Loads an MP in an octet array, most significant octet
198 * first. High-end octets are ignored if there isn't enough
202 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
206 const octet
*p
= pp
, *q
= p
+ sz
;
215 if (bits
>= MPW_BITS
) {
217 w
= n
>> (MPW_BITS
- bits
+ 8);
227 /*----- Logical shifting --------------------------------------------------*/
229 /* --- @mpx_lsl@ --- *
231 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
232 * @const mpw *av, *avl@ = source vector base and limit
233 * @size_t n@ = number of bit positions to shift by
237 * Use: Performs a logical shift left operation on an integer.
240 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
245 /* --- Trivial special case --- */
248 MPX_COPY(dv
, dvl
, av
, avl
);
250 /* --- Single bit shifting --- */
259 *dv
++ = MPW((t
<< 1) | w
);
260 w
= t
>> (MPW_BITS
- 1);
269 /* --- Break out word and bit shifts for more sophisticated work --- */
274 /* --- Handle a shift by a multiple of the word size --- */
277 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
278 memset(dv
, 0, MPWS(nw
));
281 /* --- And finally the difficult case --- *
283 * This is a little convoluted, because I have to start from the end and
284 * work backwards to avoid overwriting the source, if they're both the same
290 size_t nr
= MPW_BITS
- nb
;
291 size_t dvn
= dvl
- dv
;
292 size_t avn
= avl
- av
;
299 if (dvn
> avn
+ nw
) {
300 size_t off
= avn
+ nw
+ 1;
301 MPX_ZERO(dv
+ off
, dvl
);
311 *--dvl
= (t
>> nr
) | w
;
322 /* --- @mpx_lsr@ --- *
324 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
325 * @const mpw *av, *avl@ = source vector base and limit
326 * @size_t n@ = number of bit positions to shift by
330 * Use: Performs a logical shift right operation on an integer.
333 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
338 /* --- Trivial special case --- */
341 MPX_COPY(dv
, dvl
, av
, avl
);
343 /* --- Single bit shifting --- */
352 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
362 /* --- Break out word and bit shifts for more sophisticated work --- */
367 /* --- Handle a shift by a multiple of the word size --- */
370 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
372 /* --- And finally the difficult case --- */
376 size_t nr
= MPW_BITS
- nb
;
385 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
389 *dv
++ = MPW(w
>> nb
);
397 /*----- Unsigned arithmetic -----------------------------------------------*/
399 /* --- @mpx_2c@ --- *
401 * Arguments: @mpw *dv, *dvl@ = destination vector
402 * @const mpw *v, *vl@ = source vector
406 * Use: Calculates the two's complement of @v@.
409 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
412 while (dv
< dvl
&& v
< vl
)
413 *dv
++ = c
= MPW(~*v
++);
420 MPX_UADDN(dv
, dvl
, 1);
423 /* --- @mpx_ucmp@ --- *
425 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
426 * @const mpw *bv, *bvl@ = second argument vector base and limit
428 * Returns: Less than, equal to, or greater than zero depending on
429 * whether @a@ is less than, equal to or greater than @b@,
432 * Use: Performs an unsigned integer comparison.
435 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
440 if (avl
- av
> bvl
- bv
)
442 else if (avl
- av
< bvl
- bv
)
444 else while (avl
> av
) {
445 mpw a
= *--avl
, b
= *--bvl
;
454 /* --- @mpx_uadd@ --- *
456 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
457 * @const mpw *av, *avl@ = first addend vector base and limit
458 * @const mpw *bv, *bvl@ = second addend vector base and limit
462 * Use: Performs unsigned integer addition. If the result overflows
463 * the destination vector, high-order bits are discarded. This
464 * means that two's complement addition happens more or less for
465 * free, although that's more a side-effect than anything else.
466 * The result vector may be equal to either or both source
467 * vectors, but may not otherwise overlap them.
470 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
471 const mpw
*bv
, const mpw
*bvl
)
475 while (av
< avl
|| bv
< bvl
) {
480 a
= (av
< avl
) ?
*av
++ : 0;
481 b
= (bv
< bvl
) ?
*bv
++ : 0;
482 x
= (mpd
)a
+ (mpd
)b
+ c
;
492 /* --- @mpx_uaddn@ --- *
494 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
495 * @mpw n@ = other addend
499 * Use: Adds a small integer to a multiprecision number.
502 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
504 /* --- @mpx_usub@ --- *
506 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
507 * @const mpw *av, *avl@ = first argument vector base and limit
508 * @const mpw *bv, *bvl@ = second argument vector base and limit
512 * Use: Performs unsigned integer subtraction. If the result
513 * overflows the destination vector, high-order bits are
514 * discarded. This means that two's complement subtraction
515 * happens more or less for free, althuogh that's more a side-
516 * effect than anything else. The result vector may be equal to
517 * either or both source vectors, but may not otherwise overlap
521 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
522 const mpw
*bv
, const mpw
*bvl
)
526 while (av
< avl
|| bv
< bvl
) {
531 a
= (av
< avl
) ?
*av
++ : 0;
532 b
= (bv
< bvl
) ?
*bv
++ : 0;
533 x
= (mpd
)a
- (mpd
)b
- c
;
546 /* --- @mpx_usubn@ --- *
548 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
553 * Use: Subtracts a small integer from a multiprecision number.
556 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
558 /* --- @mpx_umul@ --- *
560 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
561 * @const mpw *av, *avl@ = multiplicand vector base and limit
562 * @const mpw *bv, *bvl@ = multiplier vector base and limit
566 * Use: Performs unsigned integer multiplication. If the result
567 * overflows the desination vector, high-order bits are
568 * discarded. The result vector may not overlap the argument
569 * vectors in any way.
572 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
573 const mpw
*bv
, const mpw
*bvl
)
575 /* --- This is probably worthwhile on a multiply --- */
580 /* --- Deal with a multiply by zero --- */
587 /* --- Do the initial multiply and initialize the accumulator --- */
589 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
591 /* --- Do the remaining multiply/accumulates --- */
593 while (dv
< dvl
&& bv
< bvl
) {
603 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
607 MPX_UADDN(dvv
, dvl
, c
);
612 /* --- @mpx_umuln@ --- *
614 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
615 * @const mpw *av, *avl@ = multiplicand vector base and limit
616 * @mpw m@ = multiplier
620 * Use: Multiplies a multiprecision integer by a single-word value.
621 * The destination and source may be equal. The destination
622 * is completely cleared after use.
625 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
627 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
630 /* --- @mpx_umlan@ --- *
632 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
633 * @const mpw *av, *avl@ = multiplicand vector base and limit
634 * @mpw m@ = multiplier
638 * Use: Multiplies a multiprecision integer by a single-word value
639 * and adds the result to an accumulator.
642 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
644 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
647 /* --- @mpx_usqr@ --- *
649 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
650 * @const mpw *av, *av@ = source vector base and limit
654 * Use: Performs unsigned integer squaring. The result vector must
655 * not overlap the source vector in any way.
658 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
662 /* --- Main loop --- */
670 /* --- Stop if I've run out of destination --- */
675 /* --- Work out the square at this point in the proceedings --- */
678 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
680 c
= MPW(x
>> MPW_BITS
);
683 /* --- Now fix up the rest of the vector upwards --- */
686 while (dvv
< dvl
&& avv
< avl
) {
687 mpd x
= (mpd
)a
* (mpd
)*avv
++;
688 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
689 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
692 while (dvv
< dvl
&& c
) {
698 /* --- Get ready for the next round --- */
705 /* --- @mpx_udiv@ --- *
707 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
708 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
709 * @const mpw *dv, *dvl@ = divisor vector base and limit
710 * @mpw *sv, *svl@ = scratch workspace
714 * Use: Performs unsigned integer division. If the result overflows
715 * the quotient vector, high-order bits are discarded. (Clearly
716 * the remainder vector can't overflow.) The various vectors
717 * may not overlap in any way. Yes, I know it's a bit odd
718 * requiring the dividend to be in the result position but it
719 * does make some sense really. The remainder must have
720 * headroom for at least two extra words. The scratch space
721 * must be at least one word larger than the divisor.
724 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
725 const mpw
*dv
, const mpw
*dvl
,
732 /* --- Initialize the quotient --- */
736 /* --- Perform some sanity checks --- */
739 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
741 /* --- Normalize the divisor --- *
743 * The algorithm requires that the divisor be at least two digits long.
744 * This is easy to fix.
751 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
752 if (d
< (MPW_MAX
>> b
)) {
761 /* --- Normalize the dividend/remainder to match --- */
764 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
765 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
775 /* --- Work out the relative scales --- */
778 size_t rvn
= rvl
- rv
;
779 size_t dvn
= dvl
- dv
;
781 /* --- If the divisor is clearly larger, notice this --- */
784 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
791 /* --- Calculate the most significant quotient digit --- *
793 * Because the divisor has its top bit set, this can only happen once. The
794 * pointer arithmetic is a little contorted, to make sure that the
795 * behaviour is defined.
798 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
799 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
800 if (qvl
- qv
> scale
)
804 /* --- Now for the main loop --- */
813 /* --- Get an estimate for the next quotient digit --- */
820 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
826 /* --- Refine the estimate --- */
830 mpd yy
= (mpd
)dd
* q
;
834 yh
+= yy
>> MPW_BITS
;
837 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
846 /* --- Remove a chunk from the dividend --- */
853 /* --- Calculate the size of the chunk --- *
855 * This does the whole job of calculating @r >> scale - qd@.
858 for (svv
= rv
+ scale
, dvv
= dv
;
859 dvv
< dvl
&& svv
< rvl
;
861 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
863 x
= (mpd
)*svv
- MPW(x
) - sc
;
872 mpd x
= (mpd
)*svv
- mc
- sc
;
882 /* --- Fix if the quotient was too large --- *
884 * This doesn't seem to happen very often.
887 if (rvl
[-1] > MPW_MAX
/ 2) {
888 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
893 /* --- Done for another iteration --- */
895 if (qvl
- qv
> scale
)
902 /* --- Now fiddle with unnormalizing and things --- */
904 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
907 /* --- @mpx_udivn@ --- *
909 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
911 * @const mpw *rv, *rvl@ = dividend
912 * @mpw d@ = single-precision divisor
914 * Returns: Remainder after divison.
916 * Use: Performs a single-precision division operation.
919 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
922 size_t ql
= qvl
- qv
;
928 r
= (r
<< MPW_BITS
) | rv
[i
];
936 /*----- Test rig ----------------------------------------------------------*/
940 #include <mLib/alloc.h>
941 #include <mLib/dstr.h>
942 #include <mLib/quis.h>
943 #include <mLib/testrig.h>
947 #define ALLOC(v, vl, sz) do { \
949 mpw *_vv = xmalloc(MPWS(_sz)); \
950 mpw *_vvl = _vv + _sz; \
955 #define LOAD(v, vl, d) do { \
956 const dstr *_d = (d); \
958 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
959 mpx_loadb(_v, _vl, _d->buf, _d->len); \
964 #define MAX(x, y) ((x) > (y) ? (x) : (y))
966 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
971 fprintf(stderr
, " %02x", *p
++);
975 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
980 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
984 static int chkscan(const mpw
*v
, const mpw
*vl
,
985 const void *pp
, size_t sz
, int step
)
992 mpscan_initx(&mps
, v
, vl
);
997 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
998 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1000 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1001 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1013 static int loadstore(dstr
*v
)
1016 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1020 dstr_ensure(&d
, v
->len
);
1021 m
= xmalloc(MPWS(sz
));
1023 for (diff
= 0; diff
< sz
; diff
+= 5) {
1028 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1029 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1031 MPX_OCTETS(oct
, m
, ml
);
1032 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1033 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1034 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1038 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1039 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1041 MPX_OCTETS(oct
, m
, ml
);
1042 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1043 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1044 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1050 dumpbits("input data", v
->buf
, v
->len
);
1057 static int lsl(dstr
*v
)
1060 int n
= *(int *)v
[1].buf
;
1067 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1069 mpx_lsl(d
, dl
, a
, al
, n
);
1070 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1071 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1072 dumpmp(" a", a
, al
);
1073 dumpmp("expected", c
, cl
);
1074 dumpmp(" result", d
, dl
);
1078 free(a
); free(c
); free(d
);
1082 static int lsr(dstr
*v
)
1085 int n
= *(int *)v
[1].buf
;
1092 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1094 mpx_lsr(d
, dl
, a
, al
, n
);
1095 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1096 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1097 dumpmp(" a", a
, al
);
1098 dumpmp("expected", c
, cl
);
1099 dumpmp(" result", d
, dl
);
1103 free(a
); free(c
); free(d
);
1107 static int uadd(dstr
*v
)
1118 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1120 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1121 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1122 fprintf(stderr
, "\n*** uadd failed\n");
1123 dumpmp(" a", a
, al
);
1124 dumpmp(" b", b
, bl
);
1125 dumpmp("expected", c
, cl
);
1126 dumpmp(" result", d
, dl
);
1130 free(a
); free(b
); free(c
); free(d
);
1134 static int usub(dstr
*v
)
1145 ALLOC(d
, dl
, al
- a
);
1147 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1148 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1149 fprintf(stderr
, "\n*** usub failed\n");
1150 dumpmp(" a", a
, al
);
1151 dumpmp(" b", b
, bl
);
1152 dumpmp("expected", c
, cl
);
1153 dumpmp(" result", d
, dl
);
1157 free(a
); free(b
); free(c
); free(d
);
1161 static int umul(dstr
*v
)
1172 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1174 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1175 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1176 fprintf(stderr
, "\n*** umul failed\n");
1177 dumpmp(" a", a
, al
);
1178 dumpmp(" b", b
, bl
);
1179 dumpmp("expected", c
, cl
);
1180 dumpmp(" result", d
, dl
);
1184 free(a
); free(b
); free(c
); free(d
);
1188 static int usqr(dstr
*v
)
1197 ALLOC(d
, dl
, 2 * (al
- a
));
1199 mpx_usqr(d
, dl
, a
, al
);
1200 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
1201 fprintf(stderr
, "\n*** usqr failed\n");
1202 dumpmp(" a", a
, al
);
1203 dumpmp("expected", c
, cl
);
1204 dumpmp(" result", d
, dl
);
1208 free(a
); free(c
); free(d
);
1212 static int udiv(dstr
*v
)
1222 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1226 ALLOC(qq
, qql
, al
- a
);
1227 ALLOC(s
, sl
, (bl
- b
) + 1);
1229 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1230 if (MPX_UCMP(qq
, qql
, !=, q
, ql
) ||
1231 MPX_UCMP(a
, al
, !=, r
, rl
)) {
1232 fprintf(stderr
, "\n*** udiv failed\n");
1233 dumpmp(" divisor", b
, bl
);
1234 dumpmp("expect r", r
, rl
);
1235 dumpmp("result r", a
, al
);
1236 dumpmp("expect q", q
, ql
);
1237 dumpmp("result q", qq
, qql
);
1241 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1245 static test_chunk defs
[] = {
1246 { "load-store", loadstore
, { &type_hex
, 0 } },
1247 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1248 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1249 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1250 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1251 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1252 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1253 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1257 int main(int argc
, char *argv
[])
1259 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1266 /*----- That's all, folks -------------------------------------------------*/