3 * $Id: mpx.c,v 1.12 2002/10/06 22: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.12 2002/10/06 22:52:50 mdw
34 * Pile of changes for supporting two's complement properly.
36 * Revision 1.11 2001/04/03 19:36:05 mdw
37 * Add some simple bitwise operations so that Perl can use them.
39 * Revision 1.10 2000/10/08 12:06:12 mdw
40 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
42 * Revision 1.9 2000/06/26 07:52:50 mdw
43 * Portability fix for the bug fix.
45 * Revision 1.8 2000/06/25 12:59:02 mdw
46 * (mpx_udiv): Fix bug in quotient digit estimation.
48 * Revision 1.7 1999/12/22 15:49:07 mdw
49 * New function for division by a small integer.
51 * Revision 1.6 1999/11/20 22:43:44 mdw
52 * Integrate testing for MPX routines.
54 * Revision 1.5 1999/11/20 22:23:27 mdw
55 * Add function versions of some low-level macros with wider use.
57 * Revision 1.4 1999/11/17 18:04:09 mdw
58 * Add two's-complement functionality. Improve mpx_udiv a little by
59 * performing the multiplication of the divisor by q with the subtraction
62 * Revision 1.3 1999/11/13 01:57:31 mdw
63 * Remove stray debugging code.
65 * Revision 1.2 1999/11/13 01:50:59 mdw
66 * Multiprecision routines finished and tested.
68 * Revision 1.1 1999/09/03 08:41:12 mdw
73 /*----- Header files ------------------------------------------------------*/
80 #include <mLib/bits.h>
85 /*----- Loading and storing -----------------------------------------------*/
87 /* --- @mpx_storel@ --- *
89 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
90 * @void *pp@ = pointer to octet array
91 * @size_t sz@ = size of octet array
95 * Use: Stores an MP in an octet array, least significant octet
96 * first. High-end octets are silently discarded if there
97 * isn't enough space for them.
100 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
103 octet
*p
= pp
, *q
= p
+ sz
;
113 *p
++ = U8(w
| n
<< bits
);
115 bits
+= MPW_BITS
- 8;
125 /* --- @mpx_loadl@ --- *
127 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
128 * @const void *pp@ = pointer to octet array
129 * @size_t sz@ = size of octet array
133 * Use: Loads an MP in an octet array, least significant octet
134 * first. High-end octets are ignored if there isn't enough
138 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
142 const octet
*p
= pp
, *q
= p
+ sz
;
151 if (bits
>= MPW_BITS
) {
153 w
= n
>> (MPW_BITS
- bits
+ 8);
163 /* --- @mpx_storeb@ --- *
165 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
166 * @void *pp@ = pointer to octet array
167 * @size_t sz@ = size of octet array
171 * Use: Stores an MP in an octet array, most significant octet
172 * first. High-end octets are silently discarded if there
173 * isn't enough space for them.
176 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
179 octet
*p
= pp
, *q
= p
+ sz
;
189 *--q
= U8(w
| n
<< bits
);
191 bits
+= MPW_BITS
- 8;
201 /* --- @mpx_loadb@ --- *
203 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
204 * @const void *pp@ = pointer to octet array
205 * @size_t sz@ = size of octet array
209 * Use: Loads an MP in an octet array, most significant octet
210 * first. High-end octets are ignored if there isn't enough
214 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
218 const octet
*p
= pp
, *q
= p
+ sz
;
227 if (bits
>= MPW_BITS
) {
229 w
= n
>> (MPW_BITS
- bits
+ 8);
239 /* --- @mpx_storel2cn@ --- *
241 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
242 * @void *pp@ = pointer to octet array
243 * @size_t sz@ = size of octet array
247 * Use: Stores a negative MP in an octet array, least significant
248 * octet first, as two's complement. High-end octets are
249 * silently discarded if there isn't enough space for them.
250 * This obviously makes the output bad.
253 void mpx_storel2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
258 octet
*p
= pp
, *q
= p
+ sz
;
270 bits
+= MPW_BITS
- 8;
288 /* --- @mpx_loadl2cn@ --- *
290 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
291 * @const void *pp@ = pointer to octet array
292 * @size_t sz@ = size of octet array
296 * Use: Loads a negative MP in an octet array, least significant
297 * octet first, as two's complement. High-end octets are
298 * ignored if there isn't enough space for them. This probably
299 * means you made the wrong choice coming here.
302 void mpx_loadl2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
307 const octet
*p
= pp
, *q
= p
+ sz
;
317 if (bits
>= MPW_BITS
) {
319 w
= n
>> (MPW_BITS
- bits
+ 8);
329 /* --- @mpx_storeb2cn@ --- *
331 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
332 * @void *pp@ = pointer to octet array
333 * @size_t sz@ = size of octet array
337 * Use: Stores a negative MP in an octet array, most significant
338 * octet first, as two's complement. High-end octets are
339 * silently discarded if there isn't enough space for them,
340 * which probably isn't what you meant.
343 void mpx_storeb2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
348 octet
*p
= pp
, *q
= p
+ sz
;
360 bits
+= MPW_BITS
- 8;
378 /* --- @mpx_loadb2cn@ --- *
380 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
381 * @const void *pp@ = pointer to octet array
382 * @size_t sz@ = size of octet array
386 * Use: Loads a negative MP in an octet array, most significant octet
387 * first as two's complement. High-end octets are ignored if
388 * there isn't enough space for them. This probably means you
389 * chose this function wrongly.
392 void mpx_loadb2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
397 const octet
*p
= pp
, *q
= p
+ sz
;
407 if (bits
>= MPW_BITS
) {
409 w
= n
>> (MPW_BITS
- bits
+ 8);
419 /*----- Logical shifting --------------------------------------------------*/
421 /* --- @mpx_lsl@ --- *
423 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
424 * @const mpw *av, *avl@ = source vector base and limit
425 * @size_t n@ = number of bit positions to shift by
429 * Use: Performs a logical shift left operation on an integer.
432 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
437 /* --- Trivial special case --- */
440 MPX_COPY(dv
, dvl
, av
, avl
);
442 /* --- Single bit shifting --- */
451 *dv
++ = MPW((t
<< 1) | w
);
452 w
= t
>> (MPW_BITS
- 1);
461 /* --- Break out word and bit shifts for more sophisticated work --- */
466 /* --- Handle a shift by a multiple of the word size --- */
469 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
470 memset(dv
, 0, MPWS(nw
));
473 /* --- And finally the difficult case --- *
475 * This is a little convoluted, because I have to start from the end and
476 * work backwards to avoid overwriting the source, if they're both the same
482 size_t nr
= MPW_BITS
- nb
;
483 size_t dvn
= dvl
- dv
;
484 size_t avn
= avl
- av
;
491 if (dvn
> avn
+ nw
) {
492 size_t off
= avn
+ nw
+ 1;
493 MPX_ZERO(dv
+ off
, dvl
);
503 *--dvl
= (t
>> nr
) | w
;
514 /* --- @mpx_lsr@ --- *
516 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
517 * @const mpw *av, *avl@ = source vector base and limit
518 * @size_t n@ = number of bit positions to shift by
522 * Use: Performs a logical shift right operation on an integer.
525 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
530 /* --- Trivial special case --- */
533 MPX_COPY(dv
, dvl
, av
, avl
);
535 /* --- Single bit shifting --- */
544 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
554 /* --- Break out word and bit shifts for more sophisticated work --- */
559 /* --- Handle a shift by a multiple of the word size --- */
562 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
564 /* --- And finally the difficult case --- */
568 size_t nr
= MPW_BITS
- nb
;
577 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
581 *dv
++ = MPW(w
>> nb
);
589 /*----- Bitwise operations ------------------------------------------------*/
591 /* --- @mpx_bitop@ --- *
593 * Arguments: @mpw *dv, *dvl@ = destination vector
594 * @const mpw *av, *avl@ = first source vector
595 * @const mpw *bv, *bvl@ = second source vector
599 * Use; Provides the dyadic boolean functions.
602 #define MPX_BITBINOP(string) \
604 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
605 const mpw *bv, const mpw *bvl) \
607 MPX_SHRINK(av, avl); \
608 MPX_SHRINK(bv, bvl); \
612 a = (av < avl) ? *av++ : 0; \
613 b = (bv < bvl) ? *bv++ : 0; \
614 *dv++ = MPX_B##string(a, b); \
618 MPX_DOBIN(MPX_BITBINOP
)
620 void mpx_not(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
626 a
= (av
< avl
) ?
*av
++ : 0;
631 /*----- Unsigned arithmetic -----------------------------------------------*/
633 /* --- @mpx_2c@ --- *
635 * Arguments: @mpw *dv, *dvl@ = destination vector
636 * @const mpw *v, *vl@ = source vector
640 * Use: Calculates the two's complement of @v@.
643 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
646 while (dv
< dvl
&& v
< vl
)
647 *dv
++ = c
= MPW(~*v
++);
654 MPX_UADDN(dv
, dvl
, 1);
657 /* --- @mpx_ueq@ --- *
659 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
660 * @const mpw *bv, *bvl@ = second argument vector base and limit
662 * Returns: Nonzero if the two vectors are equal.
664 * Use: Performs an unsigned integer test for equality.
667 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
671 if (avl
- av
!= bvl
- bv
)
680 /* --- @mpx_ucmp@ --- *
682 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
683 * @const mpw *bv, *bvl@ = second argument vector base and limit
685 * Returns: Less than, equal to, or greater than zero depending on
686 * whether @a@ is less than, equal to or greater than @b@,
689 * Use: Performs an unsigned integer comparison.
692 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
697 if (avl
- av
> bvl
- bv
)
699 else if (avl
- av
< bvl
- bv
)
701 else while (avl
> av
) {
702 mpw a
= *--avl
, b
= *--bvl
;
711 /* --- @mpx_uadd@ --- *
713 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
714 * @const mpw *av, *avl@ = first addend vector base and limit
715 * @const mpw *bv, *bvl@ = second addend vector base and limit
719 * Use: Performs unsigned integer addition. If the result overflows
720 * the destination vector, high-order bits are discarded. This
721 * means that two's complement addition happens more or less for
722 * free, although that's more a side-effect than anything else.
723 * The result vector may be equal to either or both source
724 * vectors, but may not otherwise overlap them.
727 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
728 const mpw
*bv
, const mpw
*bvl
)
732 while (av
< avl
|| bv
< bvl
) {
737 a
= (av
< avl
) ?
*av
++ : 0;
738 b
= (bv
< bvl
) ?
*bv
++ : 0;
739 x
= (mpd
)a
+ (mpd
)b
+ c
;
749 /* --- @mpx_uaddn@ --- *
751 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
752 * @mpw n@ = other addend
756 * Use: Adds a small integer to a multiprecision number.
759 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
761 /* --- @mpx_usub@ --- *
763 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
764 * @const mpw *av, *avl@ = first argument vector base and limit
765 * @const mpw *bv, *bvl@ = second argument vector base and limit
769 * Use: Performs unsigned integer subtraction. If the result
770 * overflows the destination vector, high-order bits are
771 * discarded. This means that two's complement subtraction
772 * happens more or less for free, althuogh that's more a side-
773 * effect than anything else. The result vector may be equal to
774 * either or both source vectors, but may not otherwise overlap
778 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
779 const mpw
*bv
, const mpw
*bvl
)
783 while (av
< avl
|| bv
< bvl
) {
788 a
= (av
< avl
) ?
*av
++ : 0;
789 b
= (bv
< bvl
) ?
*bv
++ : 0;
790 x
= (mpd
)a
- (mpd
)b
- c
;
803 /* --- @mpx_usubn@ --- *
805 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
810 * Use: Subtracts a small integer from a multiprecision number.
813 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
815 /* --- @mpx_umul@ --- *
817 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
818 * @const mpw *av, *avl@ = multiplicand vector base and limit
819 * @const mpw *bv, *bvl@ = multiplier vector base and limit
823 * Use: Performs unsigned integer multiplication. If the result
824 * overflows the desination vector, high-order bits are
825 * discarded. The result vector may not overlap the argument
826 * vectors in any way.
829 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
830 const mpw
*bv
, const mpw
*bvl
)
832 /* --- This is probably worthwhile on a multiply --- */
837 /* --- Deal with a multiply by zero --- */
844 /* --- Do the initial multiply and initialize the accumulator --- */
846 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
848 /* --- Do the remaining multiply/accumulates --- */
850 while (dv
< dvl
&& bv
< bvl
) {
860 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
864 MPX_UADDN(dvv
, dvl
, c
);
869 /* --- @mpx_umuln@ --- *
871 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
872 * @const mpw *av, *avl@ = multiplicand vector base and limit
873 * @mpw m@ = multiplier
877 * Use: Multiplies a multiprecision integer by a single-word value.
878 * The destination and source may be equal. The destination
879 * is completely cleared after use.
882 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
884 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
887 /* --- @mpx_umlan@ --- *
889 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
890 * @const mpw *av, *avl@ = multiplicand vector base and limit
891 * @mpw m@ = multiplier
895 * Use: Multiplies a multiprecision integer by a single-word value
896 * and adds the result to an accumulator.
899 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
901 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
904 /* --- @mpx_usqr@ --- *
906 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
907 * @const mpw *av, *av@ = source vector base and limit
911 * Use: Performs unsigned integer squaring. The result vector must
912 * not overlap the source vector in any way.
915 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
919 /* --- Main loop --- */
927 /* --- Stop if I've run out of destination --- */
932 /* --- Work out the square at this point in the proceedings --- */
935 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
937 c
= MPW(x
>> MPW_BITS
);
940 /* --- Now fix up the rest of the vector upwards --- */
943 while (dvv
< dvl
&& avv
< avl
) {
944 mpd x
= (mpd
)a
* (mpd
)*avv
++;
945 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
946 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
949 while (dvv
< dvl
&& c
) {
955 /* --- Get ready for the next round --- */
962 /* --- @mpx_udiv@ --- *
964 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
965 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
966 * @const mpw *dv, *dvl@ = divisor vector base and limit
967 * @mpw *sv, *svl@ = scratch workspace
971 * Use: Performs unsigned integer division. If the result overflows
972 * the quotient vector, high-order bits are discarded. (Clearly
973 * the remainder vector can't overflow.) The various vectors
974 * may not overlap in any way. Yes, I know it's a bit odd
975 * requiring the dividend to be in the result position but it
976 * does make some sense really. The remainder must have
977 * headroom for at least two extra words. The scratch space
978 * must be at least one word larger than the divisor.
981 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
982 const mpw
*dv
, const mpw
*dvl
,
989 /* --- Initialize the quotient --- */
993 /* --- Perform some sanity checks --- */
996 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
998 /* --- Normalize the divisor --- *
1000 * The algorithm requires that the divisor be at least two digits long.
1001 * This is easy to fix.
1008 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
1009 if (d
< (MPW_MAX
>> b
)) {
1018 /* --- Normalize the dividend/remainder to match --- */
1021 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1022 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1025 MPX_SHRINK(dv
, dvl
);
1028 MPX_SHRINK(rv
, rvl
);
1032 /* --- Work out the relative scales --- */
1035 size_t rvn
= rvl
- rv
;
1036 size_t dvn
= dvl
- dv
;
1038 /* --- If the divisor is clearly larger, notice this --- */
1041 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1048 /* --- Calculate the most significant quotient digit --- *
1050 * Because the divisor has its top bit set, this can only happen once. The
1051 * pointer arithmetic is a little contorted, to make sure that the
1052 * behaviour is defined.
1055 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1056 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1057 if (qvl
- qv
> scale
)
1061 /* --- Now for the main loop --- */
1070 /* --- Get an estimate for the next quotient digit --- */
1077 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1083 /* --- Refine the estimate --- */
1086 mpd yh
= (mpd
)d
* q
;
1087 mpd yy
= (mpd
)dd
* q
;
1091 yh
+= yy
>> MPW_BITS
;
1094 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1103 /* --- Remove a chunk from the dividend --- */
1110 /* --- Calculate the size of the chunk --- *
1112 * This does the whole job of calculating @r >> scale - qd@.
1115 for (svv
= rv
+ scale
, dvv
= dv
;
1116 dvv
< dvl
&& svv
< rvl
;
1118 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1120 x
= (mpd
)*svv
- MPW(x
) - sc
;
1129 mpd x
= (mpd
)*svv
- mc
- sc
;
1139 /* --- Fix if the quotient was too large --- *
1141 * This doesn't seem to happen very often.
1144 if (rvl
[-1] > MPW_MAX
/ 2) {
1145 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1150 /* --- Done for another iteration --- */
1152 if (qvl
- qv
> scale
)
1159 /* --- Now fiddle with unnormalizing and things --- */
1161 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1164 /* --- @mpx_udivn@ --- *
1166 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1168 * @const mpw *rv, *rvl@ = dividend
1169 * @mpw d@ = single-precision divisor
1171 * Returns: Remainder after divison.
1173 * Use: Performs a single-precision division operation.
1176 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1179 size_t ql
= qvl
- qv
;
1185 r
= (r
<< MPW_BITS
) | rv
[i
];
1193 /*----- Test rig ----------------------------------------------------------*/
1197 #include <mLib/alloc.h>
1198 #include <mLib/dstr.h>
1199 #include <mLib/quis.h>
1200 #include <mLib/testrig.h>
1204 #define ALLOC(v, vl, sz) do { \
1205 size_t _sz = (sz); \
1206 mpw *_vv = xmalloc(MPWS(_sz)); \
1207 mpw *_vvl = _vv + _sz; \
1212 #define LOAD(v, vl, d) do { \
1213 const dstr *_d = (d); \
1215 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1216 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1221 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1223 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1225 const octet
*p
= pp
;
1228 fprintf(stderr
, " %02x", *p
++);
1229 fputc('\n', stderr
);
1232 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1237 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1238 fputc('\n', stderr
);
1241 static int chkscan(const mpw
*v
, const mpw
*vl
,
1242 const void *pp
, size_t sz
, int step
)
1245 const octet
*p
= pp
;
1249 mpscan_initx(&mps
, v
, vl
);
1254 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1255 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1257 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1258 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1270 static int loadstore(dstr
*v
)
1273 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1277 dstr_ensure(&d
, v
->len
);
1278 m
= xmalloc(MPWS(sz
));
1280 for (diff
= 0; diff
< sz
; diff
+= 5) {
1285 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1286 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1288 MPX_OCTETS(oct
, m
, ml
);
1289 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1290 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1291 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1295 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1296 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1298 MPX_OCTETS(oct
, m
, ml
);
1299 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1300 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1301 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1307 dumpbits("input data", v
->buf
, v
->len
);
1314 static int twocl(dstr
*v
)
1321 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1322 dstr_ensure(&d
, sz
);
1325 m
= xmalloc(MPWS(sz
));
1328 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1329 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1330 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1331 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1335 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1336 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1337 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1338 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1343 dumpbits("pos", v
[0].buf
, v
[0].len
);
1344 dumpbits("neg", v
[1].buf
, v
[1].len
);
1353 static int twocb(dstr
*v
)
1360 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1361 dstr_ensure(&d
, sz
);
1364 m
= xmalloc(MPWS(sz
));
1367 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1368 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1369 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1370 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1374 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1375 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1376 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1377 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1382 dumpbits("pos", v
[0].buf
, v
[0].len
);
1383 dumpbits("neg", v
[1].buf
, v
[1].len
);
1392 static int lsl(dstr
*v
)
1395 int n
= *(int *)v
[1].buf
;
1402 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1404 mpx_lsl(d
, dl
, a
, al
, n
);
1405 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1406 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1407 dumpmp(" a", a
, al
);
1408 dumpmp("expected", c
, cl
);
1409 dumpmp(" result", d
, dl
);
1413 free(a
); free(c
); free(d
);
1417 static int lsr(dstr
*v
)
1420 int n
= *(int *)v
[1].buf
;
1427 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1429 mpx_lsr(d
, dl
, a
, al
, n
);
1430 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1431 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1432 dumpmp(" a", a
, al
);
1433 dumpmp("expected", c
, cl
);
1434 dumpmp(" result", d
, dl
);
1438 free(a
); free(c
); free(d
);
1442 static int uadd(dstr
*v
)
1453 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1455 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1456 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1457 fprintf(stderr
, "\n*** uadd failed\n");
1458 dumpmp(" a", a
, al
);
1459 dumpmp(" b", b
, bl
);
1460 dumpmp("expected", c
, cl
);
1461 dumpmp(" result", d
, dl
);
1465 free(a
); free(b
); free(c
); free(d
);
1469 static int usub(dstr
*v
)
1480 ALLOC(d
, dl
, al
- a
);
1482 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1483 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1484 fprintf(stderr
, "\n*** usub failed\n");
1485 dumpmp(" a", a
, al
);
1486 dumpmp(" b", b
, bl
);
1487 dumpmp("expected", c
, cl
);
1488 dumpmp(" result", d
, dl
);
1492 free(a
); free(b
); free(c
); free(d
);
1496 static int umul(dstr
*v
)
1507 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1509 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1510 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1511 fprintf(stderr
, "\n*** umul failed\n");
1512 dumpmp(" a", a
, al
);
1513 dumpmp(" b", b
, bl
);
1514 dumpmp("expected", c
, cl
);
1515 dumpmp(" result", d
, dl
);
1519 free(a
); free(b
); free(c
); free(d
);
1523 static int usqr(dstr
*v
)
1532 ALLOC(d
, dl
, 2 * (al
- a
));
1534 mpx_usqr(d
, dl
, a
, al
);
1535 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1536 fprintf(stderr
, "\n*** usqr failed\n");
1537 dumpmp(" a", a
, al
);
1538 dumpmp("expected", c
, cl
);
1539 dumpmp(" result", d
, dl
);
1543 free(a
); free(c
); free(d
);
1547 static int udiv(dstr
*v
)
1557 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1561 ALLOC(qq
, qql
, al
- a
);
1562 ALLOC(s
, sl
, (bl
- b
) + 1);
1564 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1565 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1566 !mpx_ueq(a
, al
, r
, rl
)) {
1567 fprintf(stderr
, "\n*** udiv failed\n");
1568 dumpmp(" divisor", b
, bl
);
1569 dumpmp("expect r", r
, rl
);
1570 dumpmp("result r", a
, al
);
1571 dumpmp("expect q", q
, ql
);
1572 dumpmp("result q", qq
, qql
);
1576 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1580 static test_chunk defs
[] = {
1581 { "load-store", loadstore
, { &type_hex
, 0 } },
1582 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1583 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1584 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1585 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1586 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1587 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1588 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1589 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1590 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1594 int main(int argc
, char *argv
[])
1596 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1602 /*----- That's all, folks -------------------------------------------------*/