3 * $Id: mpx.c,v 1.14 2002/10/19 18:55:08 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.14 2002/10/19 18:55:08 mdw
34 * Fix overflows in shift primitives.
36 * Revision 1.13 2002/10/19 17:56:50 mdw
37 * Fix bit operations. Test them (a bit) better.
39 * Revision 1.12 2002/10/06 22:52:50 mdw
40 * Pile of changes for supporting two's complement properly.
42 * Revision 1.11 2001/04/03 19:36:05 mdw
43 * Add some simple bitwise operations so that Perl can use them.
45 * Revision 1.10 2000/10/08 12:06:12 mdw
46 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
48 * Revision 1.9 2000/06/26 07:52:50 mdw
49 * Portability fix for the bug fix.
51 * Revision 1.8 2000/06/25 12:59:02 mdw
52 * (mpx_udiv): Fix bug in quotient digit estimation.
54 * Revision 1.7 1999/12/22 15:49:07 mdw
55 * New function for division by a small integer.
57 * Revision 1.6 1999/11/20 22:43:44 mdw
58 * Integrate testing for MPX routines.
60 * Revision 1.5 1999/11/20 22:23:27 mdw
61 * Add function versions of some low-level macros with wider use.
63 * Revision 1.4 1999/11/17 18:04:09 mdw
64 * Add two's-complement functionality. Improve mpx_udiv a little by
65 * performing the multiplication of the divisor by q with the subtraction
68 * Revision 1.3 1999/11/13 01:57:31 mdw
69 * Remove stray debugging code.
71 * Revision 1.2 1999/11/13 01:50:59 mdw
72 * Multiprecision routines finished and tested.
74 * Revision 1.1 1999/09/03 08:41:12 mdw
79 /*----- Header files ------------------------------------------------------*/
86 #include <mLib/bits.h>
92 /*----- Loading and storing -----------------------------------------------*/
94 /* --- @mpx_storel@ --- *
96 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
97 * @void *pp@ = pointer to octet array
98 * @size_t sz@ = size of octet array
102 * Use: Stores an MP in an octet array, least significant octet
103 * first. High-end octets are silently discarded if there
104 * isn't enough space for them.
107 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
110 octet
*p
= pp
, *q
= p
+ sz
;
120 *p
++ = U8(w
| n
<< bits
);
122 bits
+= MPW_BITS
- 8;
132 /* --- @mpx_loadl@ --- *
134 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
135 * @const void *pp@ = pointer to octet array
136 * @size_t sz@ = size of octet array
140 * Use: Loads an MP in an octet array, least significant octet
141 * first. High-end octets are ignored if there isn't enough
145 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
149 const octet
*p
= pp
, *q
= p
+ sz
;
158 if (bits
>= MPW_BITS
) {
160 w
= n
>> (MPW_BITS
- bits
+ 8);
170 /* --- @mpx_storeb@ --- *
172 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
173 * @void *pp@ = pointer to octet array
174 * @size_t sz@ = size of octet array
178 * Use: Stores an MP in an octet array, most significant octet
179 * first. High-end octets are silently discarded if there
180 * isn't enough space for them.
183 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
186 octet
*p
= pp
, *q
= p
+ sz
;
196 *--q
= U8(w
| n
<< bits
);
198 bits
+= MPW_BITS
- 8;
208 /* --- @mpx_loadb@ --- *
210 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
211 * @const void *pp@ = pointer to octet array
212 * @size_t sz@ = size of octet array
216 * Use: Loads an MP in an octet array, most significant octet
217 * first. High-end octets are ignored if there isn't enough
221 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
225 const octet
*p
= pp
, *q
= p
+ sz
;
234 if (bits
>= MPW_BITS
) {
236 w
= n
>> (MPW_BITS
- bits
+ 8);
246 /* --- @mpx_storel2cn@ --- *
248 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
249 * @void *pp@ = pointer to octet array
250 * @size_t sz@ = size of octet array
254 * Use: Stores a negative MP in an octet array, least significant
255 * octet first, as two's complement. High-end octets are
256 * silently discarded if there isn't enough space for them.
257 * This obviously makes the output bad.
260 void mpx_storel2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
265 octet
*p
= pp
, *q
= p
+ sz
;
277 bits
+= MPW_BITS
- 8;
295 /* --- @mpx_loadl2cn@ --- *
297 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
298 * @const void *pp@ = pointer to octet array
299 * @size_t sz@ = size of octet array
303 * Use: Loads a negative MP in an octet array, least significant
304 * octet first, as two's complement. High-end octets are
305 * ignored if there isn't enough space for them. This probably
306 * means you made the wrong choice coming here.
309 void mpx_loadl2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
314 const octet
*p
= pp
, *q
= p
+ sz
;
324 if (bits
>= MPW_BITS
) {
326 w
= n
>> (MPW_BITS
- bits
+ 8);
336 /* --- @mpx_storeb2cn@ --- *
338 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
339 * @void *pp@ = pointer to octet array
340 * @size_t sz@ = size of octet array
344 * Use: Stores a negative MP in an octet array, most significant
345 * octet first, as two's complement. High-end octets are
346 * silently discarded if there isn't enough space for them,
347 * which probably isn't what you meant.
350 void mpx_storeb2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
355 octet
*p
= pp
, *q
= p
+ sz
;
367 bits
+= MPW_BITS
- 8;
385 /* --- @mpx_loadb2cn@ --- *
387 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
388 * @const void *pp@ = pointer to octet array
389 * @size_t sz@ = size of octet array
393 * Use: Loads a negative MP in an octet array, most significant octet
394 * first as two's complement. High-end octets are ignored if
395 * there isn't enough space for them. This probably means you
396 * chose this function wrongly.
399 void mpx_loadb2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
404 const octet
*p
= pp
, *q
= p
+ sz
;
414 if (bits
>= MPW_BITS
) {
416 w
= n
>> (MPW_BITS
- bits
+ 8);
426 /*----- Logical shifting --------------------------------------------------*/
428 /* --- @mpx_lsl@ --- *
430 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
431 * @const mpw *av, *avl@ = source vector base and limit
432 * @size_t n@ = number of bit positions to shift by
436 * Use: Performs a logical shift left operation on an integer.
439 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
444 /* --- Trivial special case --- */
447 MPX_COPY(dv
, dvl
, av
, avl
);
449 /* --- Single bit shifting --- */
458 *dv
++ = MPW((t
<< 1) | w
);
459 w
= t
>> (MPW_BITS
- 1);
468 /* --- Break out word and bit shifts for more sophisticated work --- */
473 /* --- Handle a shift by a multiple of the word size --- */
479 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
480 memset(dv
, 0, MPWS(nw
));
484 /* --- And finally the difficult case --- *
486 * This is a little convoluted, because I have to start from the end and
487 * work backwards to avoid overwriting the source, if they're both the same
493 size_t nr
= MPW_BITS
- nb
;
494 size_t dvn
= dvl
- dv
;
495 size_t avn
= avl
- av
;
502 if (dvn
> avn
+ nw
) {
503 size_t off
= avn
+ nw
+ 1;
504 MPX_ZERO(dv
+ off
, dvl
);
514 *--dvl
= (t
>> nr
) | w
;
525 /* --- @mpx_lsr@ --- *
527 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
528 * @const mpw *av, *avl@ = source vector base and limit
529 * @size_t n@ = number of bit positions to shift by
533 * Use: Performs a logical shift right operation on an integer.
536 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
541 /* --- Trivial special case --- */
544 MPX_COPY(dv
, dvl
, av
, avl
);
546 /* --- Single bit shifting --- */
555 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
565 /* --- Break out word and bit shifts for more sophisticated work --- */
570 /* --- Handle a shift by a multiple of the word size --- */
576 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
579 /* --- And finally the difficult case --- */
583 size_t nr
= MPW_BITS
- nb
;
586 w
= av
< avl ?
*av
++ : 0;
592 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
596 *dv
++ = MPW(w
>> nb
);
604 /*----- Bitwise operations ------------------------------------------------*/
606 /* --- @mpx_bitop@ --- *
608 * Arguments: @mpw *dv, *dvl@ = destination vector
609 * @const mpw *av, *avl@ = first source vector
610 * @const mpw *bv, *bvl@ = second source vector
614 * Use; Provides the dyadic boolean functions.
617 #define MPX_BITBINOP(string) \
619 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
620 const mpw *bv, const mpw *bvl) \
622 MPX_SHRINK(av, avl); \
623 MPX_SHRINK(bv, bvl); \
627 a = (av < avl) ? *av++ : 0; \
628 b = (bv < bvl) ? *bv++ : 0; \
629 *dv++ = B##string(a, b); \
633 MPX_DOBIN(MPX_BITBINOP
)
635 void mpx_not(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
641 a
= (av
< avl
) ?
*av
++ : 0;
646 /*----- Unsigned arithmetic -----------------------------------------------*/
648 /* --- @mpx_2c@ --- *
650 * Arguments: @mpw *dv, *dvl@ = destination vector
651 * @const mpw *v, *vl@ = source vector
655 * Use: Calculates the two's complement of @v@.
658 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
661 while (dv
< dvl
&& v
< vl
)
662 *dv
++ = c
= MPW(~*v
++);
669 MPX_UADDN(dv
, dvl
, 1);
672 /* --- @mpx_ueq@ --- *
674 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
675 * @const mpw *bv, *bvl@ = second argument vector base and limit
677 * Returns: Nonzero if the two vectors are equal.
679 * Use: Performs an unsigned integer test for equality.
682 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
686 if (avl
- av
!= bvl
- bv
)
695 /* --- @mpx_ucmp@ --- *
697 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
698 * @const mpw *bv, *bvl@ = second argument vector base and limit
700 * Returns: Less than, equal to, or greater than zero depending on
701 * whether @a@ is less than, equal to or greater than @b@,
704 * Use: Performs an unsigned integer comparison.
707 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
712 if (avl
- av
> bvl
- bv
)
714 else if (avl
- av
< bvl
- bv
)
716 else while (avl
> av
) {
717 mpw a
= *--avl
, b
= *--bvl
;
726 /* --- @mpx_uadd@ --- *
728 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
729 * @const mpw *av, *avl@ = first addend vector base and limit
730 * @const mpw *bv, *bvl@ = second addend vector base and limit
734 * Use: Performs unsigned integer addition. If the result overflows
735 * the destination vector, high-order bits are discarded. This
736 * means that two's complement addition happens more or less for
737 * free, although that's more a side-effect than anything else.
738 * The result vector may be equal to either or both source
739 * vectors, but may not otherwise overlap them.
742 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
743 const mpw
*bv
, const mpw
*bvl
)
747 while (av
< avl
|| bv
< bvl
) {
752 a
= (av
< avl
) ?
*av
++ : 0;
753 b
= (bv
< bvl
) ?
*bv
++ : 0;
754 x
= (mpd
)a
+ (mpd
)b
+ c
;
764 /* --- @mpx_uaddn@ --- *
766 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
767 * @mpw n@ = other addend
771 * Use: Adds a small integer to a multiprecision number.
774 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
776 /* --- @mpx_usub@ --- *
778 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
779 * @const mpw *av, *avl@ = first argument vector base and limit
780 * @const mpw *bv, *bvl@ = second argument vector base and limit
784 * Use: Performs unsigned integer subtraction. If the result
785 * overflows the destination vector, high-order bits are
786 * discarded. This means that two's complement subtraction
787 * happens more or less for free, althuogh that's more a side-
788 * effect than anything else. The result vector may be equal to
789 * either or both source vectors, but may not otherwise overlap
793 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
794 const mpw
*bv
, const mpw
*bvl
)
798 while (av
< avl
|| bv
< bvl
) {
803 a
= (av
< avl
) ?
*av
++ : 0;
804 b
= (bv
< bvl
) ?
*bv
++ : 0;
805 x
= (mpd
)a
- (mpd
)b
- c
;
818 /* --- @mpx_usubn@ --- *
820 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
825 * Use: Subtracts a small integer from a multiprecision number.
828 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
830 /* --- @mpx_umul@ --- *
832 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
833 * @const mpw *av, *avl@ = multiplicand vector base and limit
834 * @const mpw *bv, *bvl@ = multiplier vector base and limit
838 * Use: Performs unsigned integer multiplication. If the result
839 * overflows the desination vector, high-order bits are
840 * discarded. The result vector may not overlap the argument
841 * vectors in any way.
844 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
845 const mpw
*bv
, const mpw
*bvl
)
847 /* --- This is probably worthwhile on a multiply --- */
852 /* --- Deal with a multiply by zero --- */
859 /* --- Do the initial multiply and initialize the accumulator --- */
861 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
863 /* --- Do the remaining multiply/accumulates --- */
865 while (dv
< dvl
&& bv
< bvl
) {
875 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
879 MPX_UADDN(dvv
, dvl
, c
);
884 /* --- @mpx_umuln@ --- *
886 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
887 * @const mpw *av, *avl@ = multiplicand vector base and limit
888 * @mpw m@ = multiplier
892 * Use: Multiplies a multiprecision integer by a single-word value.
893 * The destination and source may be equal. The destination
894 * is completely cleared after use.
897 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
899 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
902 /* --- @mpx_umlan@ --- *
904 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
905 * @const mpw *av, *avl@ = multiplicand vector base and limit
906 * @mpw m@ = multiplier
910 * Use: Multiplies a multiprecision integer by a single-word value
911 * and adds the result to an accumulator.
914 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
916 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
919 /* --- @mpx_usqr@ --- *
921 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
922 * @const mpw *av, *av@ = source vector base and limit
926 * Use: Performs unsigned integer squaring. The result vector must
927 * not overlap the source vector in any way.
930 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
934 /* --- Main loop --- */
942 /* --- Stop if I've run out of destination --- */
947 /* --- Work out the square at this point in the proceedings --- */
950 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
952 c
= MPW(x
>> MPW_BITS
);
955 /* --- Now fix up the rest of the vector upwards --- */
958 while (dvv
< dvl
&& avv
< avl
) {
959 mpd x
= (mpd
)a
* (mpd
)*avv
++;
960 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
961 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
964 while (dvv
< dvl
&& c
) {
970 /* --- Get ready for the next round --- */
977 /* --- @mpx_udiv@ --- *
979 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
980 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
981 * @const mpw *dv, *dvl@ = divisor vector base and limit
982 * @mpw *sv, *svl@ = scratch workspace
986 * Use: Performs unsigned integer division. If the result overflows
987 * the quotient vector, high-order bits are discarded. (Clearly
988 * the remainder vector can't overflow.) The various vectors
989 * may not overlap in any way. Yes, I know it's a bit odd
990 * requiring the dividend to be in the result position but it
991 * does make some sense really. The remainder must have
992 * headroom for at least two extra words. The scratch space
993 * must be at least one word larger than the divisor.
996 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
997 const mpw
*dv
, const mpw
*dvl
,
1004 /* --- Initialize the quotient --- */
1008 /* --- Perform some sanity checks --- */
1010 MPX_SHRINK(dv
, dvl
);
1011 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
1013 /* --- Normalize the divisor --- *
1015 * The algorithm requires that the divisor be at least two digits long.
1016 * This is easy to fix.
1023 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
1024 if (d
< (MPW_MAX
>> b
)) {
1033 /* --- Normalize the dividend/remainder to match --- */
1036 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1037 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1040 MPX_SHRINK(dv
, dvl
);
1043 MPX_SHRINK(rv
, rvl
);
1047 /* --- Work out the relative scales --- */
1050 size_t rvn
= rvl
- rv
;
1051 size_t dvn
= dvl
- dv
;
1053 /* --- If the divisor is clearly larger, notice this --- */
1056 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1063 /* --- Calculate the most significant quotient digit --- *
1065 * Because the divisor has its top bit set, this can only happen once. The
1066 * pointer arithmetic is a little contorted, to make sure that the
1067 * behaviour is defined.
1070 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1071 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1072 if (qvl
- qv
> scale
)
1076 /* --- Now for the main loop --- */
1085 /* --- Get an estimate for the next quotient digit --- */
1092 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1098 /* --- Refine the estimate --- */
1101 mpd yh
= (mpd
)d
* q
;
1102 mpd yy
= (mpd
)dd
* q
;
1106 yh
+= yy
>> MPW_BITS
;
1109 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1118 /* --- Remove a chunk from the dividend --- */
1125 /* --- Calculate the size of the chunk --- *
1127 * This does the whole job of calculating @r >> scale - qd@.
1130 for (svv
= rv
+ scale
, dvv
= dv
;
1131 dvv
< dvl
&& svv
< rvl
;
1133 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1135 x
= (mpd
)*svv
- MPW(x
) - sc
;
1144 mpd x
= (mpd
)*svv
- mc
- sc
;
1154 /* --- Fix if the quotient was too large --- *
1156 * This doesn't seem to happen very often.
1159 if (rvl
[-1] > MPW_MAX
/ 2) {
1160 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1165 /* --- Done for another iteration --- */
1167 if (qvl
- qv
> scale
)
1174 /* --- Now fiddle with unnormalizing and things --- */
1176 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1179 /* --- @mpx_udivn@ --- *
1181 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1183 * @const mpw *rv, *rvl@ = dividend
1184 * @mpw d@ = single-precision divisor
1186 * Returns: Remainder after divison.
1188 * Use: Performs a single-precision division operation.
1191 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1194 size_t ql
= qvl
- qv
;
1200 r
= (r
<< MPW_BITS
) | rv
[i
];
1208 /*----- Test rig ----------------------------------------------------------*/
1212 #include <mLib/alloc.h>
1213 #include <mLib/dstr.h>
1214 #include <mLib/quis.h>
1215 #include <mLib/testrig.h>
1219 #define ALLOC(v, vl, sz) do { \
1220 size_t _sz = (sz); \
1221 mpw *_vv = xmalloc(MPWS(_sz)); \
1222 mpw *_vvl = _vv + _sz; \
1227 #define LOAD(v, vl, d) do { \
1228 const dstr *_d = (d); \
1230 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1231 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1236 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1238 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1240 const octet
*p
= pp
;
1243 fprintf(stderr
, " %02x", *p
++);
1244 fputc('\n', stderr
);
1247 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1252 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1253 fputc('\n', stderr
);
1256 static int chkscan(const mpw
*v
, const mpw
*vl
,
1257 const void *pp
, size_t sz
, int step
)
1260 const octet
*p
= pp
;
1264 mpscan_initx(&mps
, v
, vl
);
1269 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1270 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1272 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1273 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1285 static int loadstore(dstr
*v
)
1288 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1292 dstr_ensure(&d
, v
->len
);
1293 m
= xmalloc(MPWS(sz
));
1295 for (diff
= 0; diff
< sz
; diff
+= 5) {
1300 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1301 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1303 MPX_OCTETS(oct
, m
, ml
);
1304 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1305 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1306 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1310 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1311 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1313 MPX_OCTETS(oct
, m
, ml
);
1314 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1315 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1316 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1322 dumpbits("input data", v
->buf
, v
->len
);
1329 static int twocl(dstr
*v
)
1336 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1337 dstr_ensure(&d
, sz
);
1340 m
= xmalloc(MPWS(sz
));
1343 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1344 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1345 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1346 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1350 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1351 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1352 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1353 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1358 dumpbits("pos", v
[0].buf
, v
[0].len
);
1359 dumpbits("neg", v
[1].buf
, v
[1].len
);
1368 static int twocb(dstr
*v
)
1375 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1376 dstr_ensure(&d
, sz
);
1379 m
= xmalloc(MPWS(sz
));
1382 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1383 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1384 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1385 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1389 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1390 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1391 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1392 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1397 dumpbits("pos", v
[0].buf
, v
[0].len
);
1398 dumpbits("neg", v
[1].buf
, v
[1].len
);
1407 static int lsl(dstr
*v
)
1410 int n
= *(int *)v
[1].buf
;
1417 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1419 mpx_lsl(d
, dl
, a
, al
, n
);
1420 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1421 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1422 dumpmp(" a", a
, al
);
1423 dumpmp("expected", c
, cl
);
1424 dumpmp(" result", d
, dl
);
1428 free(a
); free(c
); free(d
);
1432 static int lsr(dstr
*v
)
1435 int n
= *(int *)v
[1].buf
;
1442 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1444 mpx_lsr(d
, dl
, a
, al
, n
);
1445 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1446 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1447 dumpmp(" a", a
, al
);
1448 dumpmp("expected", c
, cl
);
1449 dumpmp(" result", d
, dl
);
1453 free(a
); free(c
); free(d
);
1457 static int uadd(dstr
*v
)
1468 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1470 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1471 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1472 fprintf(stderr
, "\n*** uadd failed\n");
1473 dumpmp(" a", a
, al
);
1474 dumpmp(" b", b
, bl
);
1475 dumpmp("expected", c
, cl
);
1476 dumpmp(" result", d
, dl
);
1480 free(a
); free(b
); free(c
); free(d
);
1484 static int usub(dstr
*v
)
1495 ALLOC(d
, dl
, al
- a
);
1497 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1498 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1499 fprintf(stderr
, "\n*** usub failed\n");
1500 dumpmp(" a", a
, al
);
1501 dumpmp(" b", b
, bl
);
1502 dumpmp("expected", c
, cl
);
1503 dumpmp(" result", d
, dl
);
1507 free(a
); free(b
); free(c
); free(d
);
1511 static int umul(dstr
*v
)
1522 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1524 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1525 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1526 fprintf(stderr
, "\n*** umul failed\n");
1527 dumpmp(" a", a
, al
);
1528 dumpmp(" b", b
, bl
);
1529 dumpmp("expected", c
, cl
);
1530 dumpmp(" result", d
, dl
);
1534 free(a
); free(b
); free(c
); free(d
);
1538 static int usqr(dstr
*v
)
1547 ALLOC(d
, dl
, 2 * (al
- a
));
1549 mpx_usqr(d
, dl
, a
, al
);
1550 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1551 fprintf(stderr
, "\n*** usqr failed\n");
1552 dumpmp(" a", a
, al
);
1553 dumpmp("expected", c
, cl
);
1554 dumpmp(" result", d
, dl
);
1558 free(a
); free(c
); free(d
);
1562 static int udiv(dstr
*v
)
1572 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1576 ALLOC(qq
, qql
, al
- a
);
1577 ALLOC(s
, sl
, (bl
- b
) + 1);
1579 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1580 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1581 !mpx_ueq(a
, al
, r
, rl
)) {
1582 fprintf(stderr
, "\n*** udiv failed\n");
1583 dumpmp(" divisor", b
, bl
);
1584 dumpmp("expect r", r
, rl
);
1585 dumpmp("result r", a
, al
);
1586 dumpmp("expect q", q
, ql
);
1587 dumpmp("result q", qq
, qql
);
1591 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1595 static test_chunk defs
[] = {
1596 { "load-store", loadstore
, { &type_hex
, 0 } },
1597 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1598 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1599 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1600 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1601 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1602 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1603 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1604 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1605 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1609 int main(int argc
, char *argv
[])
1611 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1617 /*----- That's all, folks -------------------------------------------------*/