b88bd49bf0be9f7be535080f12e346c02bde2fdf
3 * $Id: mpx.c,v 1.13 2002/10/19 17:56: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.13 2002/10/19 17:56:50 mdw
34 * Fix bit operations. Test them (a bit) better.
36 * Revision 1.12 2002/10/06 22:52:50 mdw
37 * Pile of changes for supporting two's complement properly.
39 * Revision 1.11 2001/04/03 19:36:05 mdw
40 * Add some simple bitwise operations so that Perl can use them.
42 * Revision 1.10 2000/10/08 12:06:12 mdw
43 * Provide @mpx_ueq@ for rapidly testing equality of two integers.
45 * Revision 1.9 2000/06/26 07:52:50 mdw
46 * Portability fix for the bug fix.
48 * Revision 1.8 2000/06/25 12:59:02 mdw
49 * (mpx_udiv): Fix bug in quotient digit estimation.
51 * Revision 1.7 1999/12/22 15:49:07 mdw
52 * New function for division by a small integer.
54 * Revision 1.6 1999/11/20 22:43:44 mdw
55 * Integrate testing for MPX routines.
57 * Revision 1.5 1999/11/20 22:23:27 mdw
58 * Add function versions of some low-level macros with wider use.
60 * Revision 1.4 1999/11/17 18:04:09 mdw
61 * Add two's-complement functionality. Improve mpx_udiv a little by
62 * performing the multiplication of the divisor by q with the subtraction
65 * Revision 1.3 1999/11/13 01:57:31 mdw
66 * Remove stray debugging code.
68 * Revision 1.2 1999/11/13 01:50:59 mdw
69 * Multiprecision routines finished and tested.
71 * Revision 1.1 1999/09/03 08:41:12 mdw
76 /*----- Header files ------------------------------------------------------*/
83 #include <mLib/bits.h>
89 /*----- Loading and storing -----------------------------------------------*/
91 /* --- @mpx_storel@ --- *
93 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
94 * @void *pp@ = pointer to octet array
95 * @size_t sz@ = size of octet array
99 * Use: Stores an MP in an octet array, least significant octet
100 * first. High-end octets are silently discarded if there
101 * isn't enough space for them.
104 void mpx_storel(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
107 octet
*p
= pp
, *q
= p
+ sz
;
117 *p
++ = U8(w
| n
<< bits
);
119 bits
+= MPW_BITS
- 8;
129 /* --- @mpx_loadl@ --- *
131 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
132 * @const void *pp@ = pointer to octet array
133 * @size_t sz@ = size of octet array
137 * Use: Loads an MP in an octet array, least significant octet
138 * first. High-end octets are ignored if there isn't enough
142 void mpx_loadl(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
146 const octet
*p
= pp
, *q
= p
+ sz
;
155 if (bits
>= MPW_BITS
) {
157 w
= n
>> (MPW_BITS
- bits
+ 8);
167 /* --- @mpx_storeb@ --- *
169 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
170 * @void *pp@ = pointer to octet array
171 * @size_t sz@ = size of octet array
175 * Use: Stores an MP in an octet array, most significant octet
176 * first. High-end octets are silently discarded if there
177 * isn't enough space for them.
180 void mpx_storeb(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
183 octet
*p
= pp
, *q
= p
+ sz
;
193 *--q
= U8(w
| n
<< bits
);
195 bits
+= MPW_BITS
- 8;
205 /* --- @mpx_loadb@ --- *
207 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
208 * @const void *pp@ = pointer to octet array
209 * @size_t sz@ = size of octet array
213 * Use: Loads an MP in an octet array, most significant octet
214 * first. High-end octets are ignored if there isn't enough
218 void mpx_loadb(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
222 const octet
*p
= pp
, *q
= p
+ sz
;
231 if (bits
>= MPW_BITS
) {
233 w
= n
>> (MPW_BITS
- bits
+ 8);
243 /* --- @mpx_storel2cn@ --- *
245 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
246 * @void *pp@ = pointer to octet array
247 * @size_t sz@ = size of octet array
251 * Use: Stores a negative MP in an octet array, least significant
252 * octet first, as two's complement. High-end octets are
253 * silently discarded if there isn't enough space for them.
254 * This obviously makes the output bad.
257 void mpx_storel2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
262 octet
*p
= pp
, *q
= p
+ sz
;
274 bits
+= MPW_BITS
- 8;
292 /* --- @mpx_loadl2cn@ --- *
294 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
295 * @const void *pp@ = pointer to octet array
296 * @size_t sz@ = size of octet array
300 * Use: Loads a negative MP in an octet array, least significant
301 * octet first, as two's complement. High-end octets are
302 * ignored if there isn't enough space for them. This probably
303 * means you made the wrong choice coming here.
306 void mpx_loadl2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
311 const octet
*p
= pp
, *q
= p
+ sz
;
321 if (bits
>= MPW_BITS
) {
323 w
= n
>> (MPW_BITS
- bits
+ 8);
333 /* --- @mpx_storeb2cn@ --- *
335 * Arguments: @const mpw *v, *vl@ = base and limit of source vector
336 * @void *pp@ = pointer to octet array
337 * @size_t sz@ = size of octet array
341 * Use: Stores a negative MP in an octet array, most significant
342 * octet first, as two's complement. High-end octets are
343 * silently discarded if there isn't enough space for them,
344 * which probably isn't what you meant.
347 void mpx_storeb2cn(const mpw
*v
, const mpw
*vl
, void *pp
, size_t sz
)
352 octet
*p
= pp
, *q
= p
+ sz
;
364 bits
+= MPW_BITS
- 8;
382 /* --- @mpx_loadb2cn@ --- *
384 * Arguments: @mpw *v, *vl@ = base and limit of destination vector
385 * @const void *pp@ = pointer to octet array
386 * @size_t sz@ = size of octet array
390 * Use: Loads a negative MP in an octet array, most significant octet
391 * first as two's complement. High-end octets are ignored if
392 * there isn't enough space for them. This probably means you
393 * chose this function wrongly.
396 void mpx_loadb2cn(mpw
*v
, mpw
*vl
, const void *pp
, size_t sz
)
401 const octet
*p
= pp
, *q
= p
+ sz
;
411 if (bits
>= MPW_BITS
) {
413 w
= n
>> (MPW_BITS
- bits
+ 8);
423 /*----- Logical shifting --------------------------------------------------*/
425 /* --- @mpx_lsl@ --- *
427 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
428 * @const mpw *av, *avl@ = source vector base and limit
429 * @size_t n@ = number of bit positions to shift by
433 * Use: Performs a logical shift left operation on an integer.
436 void mpx_lsl(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
441 /* --- Trivial special case --- */
444 MPX_COPY(dv
, dvl
, av
, avl
);
446 /* --- Single bit shifting --- */
455 *dv
++ = MPW((t
<< 1) | w
);
456 w
= t
>> (MPW_BITS
- 1);
465 /* --- Break out word and bit shifts for more sophisticated work --- */
470 /* --- Handle a shift by a multiple of the word size --- */
473 MPX_COPY(dv
+ nw
, dvl
, av
, avl
);
474 memset(dv
, 0, MPWS(nw
));
477 /* --- And finally the difficult case --- *
479 * This is a little convoluted, because I have to start from the end and
480 * work backwards to avoid overwriting the source, if they're both the same
486 size_t nr
= MPW_BITS
- nb
;
487 size_t dvn
= dvl
- dv
;
488 size_t avn
= avl
- av
;
495 if (dvn
> avn
+ nw
) {
496 size_t off
= avn
+ nw
+ 1;
497 MPX_ZERO(dv
+ off
, dvl
);
507 *--dvl
= (t
>> nr
) | w
;
518 /* --- @mpx_lsr@ --- *
520 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
521 * @const mpw *av, *avl@ = source vector base and limit
522 * @size_t n@ = number of bit positions to shift by
526 * Use: Performs a logical shift right operation on an integer.
529 void mpx_lsr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, size_t n
)
534 /* --- Trivial special case --- */
537 MPX_COPY(dv
, dvl
, av
, avl
);
539 /* --- Single bit shifting --- */
548 *dv
++ = MPW((t
<< (MPW_BITS
- 1)) | w
);
558 /* --- Break out word and bit shifts for more sophisticated work --- */
563 /* --- Handle a shift by a multiple of the word size --- */
566 MPX_COPY(dv
, dvl
, av
+ nw
, avl
);
568 /* --- And finally the difficult case --- */
572 size_t nr
= MPW_BITS
- nb
;
581 *dv
++ = MPW((w
>> nb
) | (t
<< nr
));
585 *dv
++ = MPW(w
>> nb
);
593 /*----- Bitwise operations ------------------------------------------------*/
595 /* --- @mpx_bitop@ --- *
597 * Arguments: @mpw *dv, *dvl@ = destination vector
598 * @const mpw *av, *avl@ = first source vector
599 * @const mpw *bv, *bvl@ = second source vector
603 * Use; Provides the dyadic boolean functions.
606 #define MPX_BITBINOP(string) \
608 void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \
609 const mpw *bv, const mpw *bvl) \
611 MPX_SHRINK(av, avl); \
612 MPX_SHRINK(bv, bvl); \
616 a = (av < avl) ? *av++ : 0; \
617 b = (bv < bvl) ? *bv++ : 0; \
618 *dv++ = B##string(a, b); \
622 MPX_DOBIN(MPX_BITBINOP
)
624 void mpx_not(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
630 a
= (av
< avl
) ?
*av
++ : 0;
635 /*----- Unsigned arithmetic -----------------------------------------------*/
637 /* --- @mpx_2c@ --- *
639 * Arguments: @mpw *dv, *dvl@ = destination vector
640 * @const mpw *v, *vl@ = source vector
644 * Use: Calculates the two's complement of @v@.
647 void mpx_2c(mpw
*dv
, mpw
*dvl
, const mpw
*v
, const mpw
*vl
)
650 while (dv
< dvl
&& v
< vl
)
651 *dv
++ = c
= MPW(~*v
++);
658 MPX_UADDN(dv
, dvl
, 1);
661 /* --- @mpx_ueq@ --- *
663 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
664 * @const mpw *bv, *bvl@ = second argument vector base and limit
666 * Returns: Nonzero if the two vectors are equal.
668 * Use: Performs an unsigned integer test for equality.
671 int mpx_ueq(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
675 if (avl
- av
!= bvl
- bv
)
684 /* --- @mpx_ucmp@ --- *
686 * Arguments: @const mpw *av, *avl@ = first argument vector base and limit
687 * @const mpw *bv, *bvl@ = second argument vector base and limit
689 * Returns: Less than, equal to, or greater than zero depending on
690 * whether @a@ is less than, equal to or greater than @b@,
693 * Use: Performs an unsigned integer comparison.
696 int mpx_ucmp(const mpw
*av
, const mpw
*avl
, const mpw
*bv
, const mpw
*bvl
)
701 if (avl
- av
> bvl
- bv
)
703 else if (avl
- av
< bvl
- bv
)
705 else while (avl
> av
) {
706 mpw a
= *--avl
, b
= *--bvl
;
715 /* --- @mpx_uadd@ --- *
717 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
718 * @const mpw *av, *avl@ = first addend vector base and limit
719 * @const mpw *bv, *bvl@ = second addend vector base and limit
723 * Use: Performs unsigned integer addition. If the result overflows
724 * the destination vector, high-order bits are discarded. This
725 * means that two's complement addition happens more or less for
726 * free, although that's more a side-effect than anything else.
727 * The result vector may be equal to either or both source
728 * vectors, but may not otherwise overlap them.
731 void mpx_uadd(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
732 const mpw
*bv
, const mpw
*bvl
)
736 while (av
< avl
|| bv
< bvl
) {
741 a
= (av
< avl
) ?
*av
++ : 0;
742 b
= (bv
< bvl
) ?
*bv
++ : 0;
743 x
= (mpd
)a
+ (mpd
)b
+ c
;
753 /* --- @mpx_uaddn@ --- *
755 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
756 * @mpw n@ = other addend
760 * Use: Adds a small integer to a multiprecision number.
763 void mpx_uaddn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_UADDN(dv
, dvl
, n
); }
765 /* --- @mpx_usub@ --- *
767 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
768 * @const mpw *av, *avl@ = first argument vector base and limit
769 * @const mpw *bv, *bvl@ = second argument vector base and limit
773 * Use: Performs unsigned integer subtraction. If the result
774 * overflows the destination vector, high-order bits are
775 * discarded. This means that two's complement subtraction
776 * happens more or less for free, althuogh that's more a side-
777 * effect than anything else. The result vector may be equal to
778 * either or both source vectors, but may not otherwise overlap
782 void mpx_usub(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
783 const mpw
*bv
, const mpw
*bvl
)
787 while (av
< avl
|| bv
< bvl
) {
792 a
= (av
< avl
) ?
*av
++ : 0;
793 b
= (bv
< bvl
) ?
*bv
++ : 0;
794 x
= (mpd
)a
- (mpd
)b
- c
;
807 /* --- @mpx_usubn@ --- *
809 * Arguments: @mpw *dv, *dvl@ = source and destination base and limit
814 * Use: Subtracts a small integer from a multiprecision number.
817 void mpx_usubn(mpw
*dv
, mpw
*dvl
, mpw n
) { MPX_USUBN(dv
, dvl
, n
); }
819 /* --- @mpx_umul@ --- *
821 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
822 * @const mpw *av, *avl@ = multiplicand vector base and limit
823 * @const mpw *bv, *bvl@ = multiplier vector base and limit
827 * Use: Performs unsigned integer multiplication. If the result
828 * overflows the desination vector, high-order bits are
829 * discarded. The result vector may not overlap the argument
830 * vectors in any way.
833 void mpx_umul(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
,
834 const mpw
*bv
, const mpw
*bvl
)
836 /* --- This is probably worthwhile on a multiply --- */
841 /* --- Deal with a multiply by zero --- */
848 /* --- Do the initial multiply and initialize the accumulator --- */
850 MPX_UMULN(dv
, dvl
, av
, avl
, *bv
++);
852 /* --- Do the remaining multiply/accumulates --- */
854 while (dv
< dvl
&& bv
< bvl
) {
864 x
= (mpd
)*dvv
+ (mpd
)m
* (mpd
)*avv
++ + c
;
868 MPX_UADDN(dvv
, dvl
, c
);
873 /* --- @mpx_umuln@ --- *
875 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
876 * @const mpw *av, *avl@ = multiplicand vector base and limit
877 * @mpw m@ = multiplier
881 * Use: Multiplies a multiprecision integer by a single-word value.
882 * The destination and source may be equal. The destination
883 * is completely cleared after use.
886 void mpx_umuln(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
888 MPX_UMULN(dv
, dvl
, av
, avl
, m
);
891 /* --- @mpx_umlan@ --- *
893 * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit
894 * @const mpw *av, *avl@ = multiplicand vector base and limit
895 * @mpw m@ = multiplier
899 * Use: Multiplies a multiprecision integer by a single-word value
900 * and adds the result to an accumulator.
903 void mpx_umlan(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
, mpw m
)
905 MPX_UMLAN(dv
, dvl
, av
, avl
, m
);
908 /* --- @mpx_usqr@ --- *
910 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
911 * @const mpw *av, *av@ = source vector base and limit
915 * Use: Performs unsigned integer squaring. The result vector must
916 * not overlap the source vector in any way.
919 void mpx_usqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
923 /* --- Main loop --- */
931 /* --- Stop if I've run out of destination --- */
936 /* --- Work out the square at this point in the proceedings --- */
939 mpd x
= (mpd
)a
* (mpd
)a
+ *dvv
;
941 c
= MPW(x
>> MPW_BITS
);
944 /* --- Now fix up the rest of the vector upwards --- */
947 while (dvv
< dvl
&& avv
< avl
) {
948 mpd x
= (mpd
)a
* (mpd
)*avv
++;
949 mpd y
= ((x
<< 1) & MPW_MAX
) + c
+ *dvv
;
950 c
= (x
>> (MPW_BITS
- 1)) + (y
>> MPW_BITS
);
953 while (dvv
< dvl
&& c
) {
959 /* --- Get ready for the next round --- */
966 /* --- @mpx_udiv@ --- *
968 * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit
969 * @mpw *rv, *rvl@ = dividend/remainder vector base and limit
970 * @const mpw *dv, *dvl@ = divisor vector base and limit
971 * @mpw *sv, *svl@ = scratch workspace
975 * Use: Performs unsigned integer division. If the result overflows
976 * the quotient vector, high-order bits are discarded. (Clearly
977 * the remainder vector can't overflow.) The various vectors
978 * may not overlap in any way. Yes, I know it's a bit odd
979 * requiring the dividend to be in the result position but it
980 * does make some sense really. The remainder must have
981 * headroom for at least two extra words. The scratch space
982 * must be at least one word larger than the divisor.
985 void mpx_udiv(mpw
*qv
, mpw
*qvl
, mpw
*rv
, mpw
*rvl
,
986 const mpw
*dv
, const mpw
*dvl
,
993 /* --- Initialize the quotient --- */
997 /* --- Perform some sanity checks --- */
1000 assert(((void)"division by zero in mpx_udiv", dv
< dvl
));
1002 /* --- Normalize the divisor --- *
1004 * The algorithm requires that the divisor be at least two digits long.
1005 * This is easy to fix.
1012 for (b
= MPW_BITS
/ 2; b
; b
>>= 1) {
1013 if (d
< (MPW_MAX
>> b
)) {
1022 /* --- Normalize the dividend/remainder to match --- */
1025 mpx_lsl(rv
, rvl
, rv
, rvl
, norm
);
1026 mpx_lsl(sv
, svl
, dv
, dvl
, norm
);
1029 MPX_SHRINK(dv
, dvl
);
1032 MPX_SHRINK(rv
, rvl
);
1036 /* --- Work out the relative scales --- */
1039 size_t rvn
= rvl
- rv
;
1040 size_t dvn
= dvl
- dv
;
1042 /* --- If the divisor is clearly larger, notice this --- */
1045 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1052 /* --- Calculate the most significant quotient digit --- *
1054 * Because the divisor has its top bit set, this can only happen once. The
1055 * pointer arithmetic is a little contorted, to make sure that the
1056 * behaviour is defined.
1059 if (MPX_UCMP(rv
+ scale
, rvl
, >=, dv
, dvl
)) {
1060 mpx_usub(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1061 if (qvl
- qv
> scale
)
1065 /* --- Now for the main loop --- */
1074 /* --- Get an estimate for the next quotient digit --- */
1081 rh
= ((mpd
)r
<< MPW_BITS
) | rr
;
1087 /* --- Refine the estimate --- */
1090 mpd yh
= (mpd
)d
* q
;
1091 mpd yy
= (mpd
)dd
* q
;
1095 yh
+= yy
>> MPW_BITS
;
1098 while (yh
> rh
|| (yh
== rh
&& yl
> rrr
)) {
1107 /* --- Remove a chunk from the dividend --- */
1114 /* --- Calculate the size of the chunk --- *
1116 * This does the whole job of calculating @r >> scale - qd@.
1119 for (svv
= rv
+ scale
, dvv
= dv
;
1120 dvv
< dvl
&& svv
< rvl
;
1122 mpd x
= (mpd
)*dvv
* (mpd
)q
+ mc
;
1124 x
= (mpd
)*svv
- MPW(x
) - sc
;
1133 mpd x
= (mpd
)*svv
- mc
- sc
;
1143 /* --- Fix if the quotient was too large --- *
1145 * This doesn't seem to happen very often.
1148 if (rvl
[-1] > MPW_MAX
/ 2) {
1149 mpx_uadd(rv
+ scale
, rvl
, rv
+ scale
, rvl
, dv
, dvl
);
1154 /* --- Done for another iteration --- */
1156 if (qvl
- qv
> scale
)
1163 /* --- Now fiddle with unnormalizing and things --- */
1165 mpx_lsr(rv
, rvl
, rv
, rvl
, norm
);
1168 /* --- @mpx_udivn@ --- *
1170 * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap
1172 * @const mpw *rv, *rvl@ = dividend
1173 * @mpw d@ = single-precision divisor
1175 * Returns: Remainder after divison.
1177 * Use: Performs a single-precision division operation.
1180 mpw
mpx_udivn(mpw
*qv
, mpw
*qvl
, const mpw
*rv
, const mpw
*rvl
, mpw d
)
1183 size_t ql
= qvl
- qv
;
1189 r
= (r
<< MPW_BITS
) | rv
[i
];
1197 /*----- Test rig ----------------------------------------------------------*/
1201 #include <mLib/alloc.h>
1202 #include <mLib/dstr.h>
1203 #include <mLib/quis.h>
1204 #include <mLib/testrig.h>
1208 #define ALLOC(v, vl, sz) do { \
1209 size_t _sz = (sz); \
1210 mpw *_vv = xmalloc(MPWS(_sz)); \
1211 mpw *_vvl = _vv + _sz; \
1216 #define LOAD(v, vl, d) do { \
1217 const dstr *_d = (d); \
1219 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
1220 mpx_loadb(_v, _vl, _d->buf, _d->len); \
1225 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1227 static void dumpbits(const char *msg
, const void *pp
, size_t sz
)
1229 const octet
*p
= pp
;
1232 fprintf(stderr
, " %02x", *p
++);
1233 fputc('\n', stderr
);
1236 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
1241 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
1242 fputc('\n', stderr
);
1245 static int chkscan(const mpw
*v
, const mpw
*vl
,
1246 const void *pp
, size_t sz
, int step
)
1249 const octet
*p
= pp
;
1253 mpscan_initx(&mps
, v
, vl
);
1258 for (i
= 0; i
< 8 && MPSCAN_STEP(&mps
); i
++) {
1259 if (MPSCAN_BIT(&mps
) != (x
& 1)) {
1261 "\n*** error, step %i, bit %u, expected %u, found %u\n",
1262 step
, bit
, x
& 1, MPSCAN_BIT(&mps
));
1274 static int loadstore(dstr
*v
)
1277 size_t sz
= MPW_RQ(v
->len
) * 2, diff
;
1281 dstr_ensure(&d
, v
->len
);
1282 m
= xmalloc(MPWS(sz
));
1284 for (diff
= 0; diff
< sz
; diff
+= 5) {
1289 mpx_loadl(m
, ml
, v
->buf
, v
->len
);
1290 if (!chkscan(m
, ml
, v
->buf
, v
->len
, +1))
1292 MPX_OCTETS(oct
, m
, ml
);
1293 mpx_storel(m
, ml
, d
.buf
, d
.sz
);
1294 if (memcmp(d
.buf
, v
->buf
, oct
) != 0) {
1295 dumpbits("\n*** storel failed", d
.buf
, d
.sz
);
1299 mpx_loadb(m
, ml
, v
->buf
, v
->len
);
1300 if (!chkscan(m
, ml
, v
->buf
+ v
->len
- 1, v
->len
, -1))
1302 MPX_OCTETS(oct
, m
, ml
);
1303 mpx_storeb(m
, ml
, d
.buf
, d
.sz
);
1304 if (memcmp(d
.buf
+ d
.sz
- oct
, v
->buf
+ v
->len
- oct
, oct
) != 0) {
1305 dumpbits("\n*** storeb failed", d
.buf
, d
.sz
);
1311 dumpbits("input data", v
->buf
, v
->len
);
1318 static int twocl(dstr
*v
)
1325 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1326 dstr_ensure(&d
, sz
);
1329 m
= xmalloc(MPWS(sz
));
1332 mpx_loadl(m
, ml
, v
[0].buf
, v
[0].len
);
1333 mpx_storel2cn(m
, ml
, d
.buf
, v
[1].len
);
1334 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1335 dumpbits("\n*** storel2cn failed", d
.buf
, v
[1].len
);
1339 mpx_loadl2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1340 mpx_storel(m
, ml
, d
.buf
, v
[0].len
);
1341 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1342 dumpbits("\n*** loadl2cn failed", d
.buf
, v
[0].len
);
1347 dumpbits("pos", v
[0].buf
, v
[0].len
);
1348 dumpbits("neg", v
[1].buf
, v
[1].len
);
1357 static int twocb(dstr
*v
)
1364 sz
= v
[0].len
; if (v
[1].len
> sz
) sz
= v
[1].len
;
1365 dstr_ensure(&d
, sz
);
1368 m
= xmalloc(MPWS(sz
));
1371 mpx_loadb(m
, ml
, v
[0].buf
, v
[0].len
);
1372 mpx_storeb2cn(m
, ml
, d
.buf
, v
[1].len
);
1373 if (memcmp(d
.buf
, v
[1].buf
, v
[1].len
)) {
1374 dumpbits("\n*** storeb2cn failed", d
.buf
, v
[1].len
);
1378 mpx_loadb2cn(m
, ml
, v
[1].buf
, v
[1].len
);
1379 mpx_storeb(m
, ml
, d
.buf
, v
[0].len
);
1380 if (memcmp(d
.buf
, v
[0].buf
, v
[0].len
)) {
1381 dumpbits("\n*** loadb2cn failed", d
.buf
, v
[0].len
);
1386 dumpbits("pos", v
[0].buf
, v
[0].len
);
1387 dumpbits("neg", v
[1].buf
, v
[1].len
);
1396 static int lsl(dstr
*v
)
1399 int n
= *(int *)v
[1].buf
;
1406 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
);
1408 mpx_lsl(d
, dl
, a
, al
, n
);
1409 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1410 fprintf(stderr
, "\n*** lsl(%i) failed\n", n
);
1411 dumpmp(" a", a
, al
);
1412 dumpmp("expected", c
, cl
);
1413 dumpmp(" result", d
, dl
);
1417 free(a
); free(c
); free(d
);
1421 static int lsr(dstr
*v
)
1424 int n
= *(int *)v
[1].buf
;
1431 ALLOC(d
, dl
, al
- a
+ (n
+ MPW_BITS
- 1) / MPW_BITS
+ 1);
1433 mpx_lsr(d
, dl
, a
, al
, n
);
1434 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1435 fprintf(stderr
, "\n*** lsr(%i) failed\n", n
);
1436 dumpmp(" a", a
, al
);
1437 dumpmp("expected", c
, cl
);
1438 dumpmp(" result", d
, dl
);
1442 free(a
); free(c
); free(d
);
1446 static int uadd(dstr
*v
)
1457 ALLOC(d
, dl
, MAX(al
- a
, bl
- b
) + 1);
1459 mpx_uadd(d
, dl
, a
, al
, b
, bl
);
1460 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1461 fprintf(stderr
, "\n*** uadd failed\n");
1462 dumpmp(" a", a
, al
);
1463 dumpmp(" b", b
, bl
);
1464 dumpmp("expected", c
, cl
);
1465 dumpmp(" result", d
, dl
);
1469 free(a
); free(b
); free(c
); free(d
);
1473 static int usub(dstr
*v
)
1484 ALLOC(d
, dl
, al
- a
);
1486 mpx_usub(d
, dl
, a
, al
, b
, bl
);
1487 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1488 fprintf(stderr
, "\n*** usub failed\n");
1489 dumpmp(" a", a
, al
);
1490 dumpmp(" b", b
, bl
);
1491 dumpmp("expected", c
, cl
);
1492 dumpmp(" result", d
, dl
);
1496 free(a
); free(b
); free(c
); free(d
);
1500 static int umul(dstr
*v
)
1511 ALLOC(d
, dl
, (al
- a
) + (bl
- b
));
1513 mpx_umul(d
, dl
, a
, al
, b
, bl
);
1514 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1515 fprintf(stderr
, "\n*** umul failed\n");
1516 dumpmp(" a", a
, al
);
1517 dumpmp(" b", b
, bl
);
1518 dumpmp("expected", c
, cl
);
1519 dumpmp(" result", d
, dl
);
1523 free(a
); free(b
); free(c
); free(d
);
1527 static int usqr(dstr
*v
)
1536 ALLOC(d
, dl
, 2 * (al
- a
));
1538 mpx_usqr(d
, dl
, a
, al
);
1539 if (!mpx_ueq(d
, dl
, c
, cl
)) {
1540 fprintf(stderr
, "\n*** usqr failed\n");
1541 dumpmp(" a", a
, al
);
1542 dumpmp("expected", c
, cl
);
1543 dumpmp(" result", d
, dl
);
1547 free(a
); free(c
); free(d
);
1551 static int udiv(dstr
*v
)
1561 ALLOC(a
, al
, MPW_RQ(v
[0].len
) + 2); mpx_loadb(a
, al
, v
[0].buf
, v
[0].len
);
1565 ALLOC(qq
, qql
, al
- a
);
1566 ALLOC(s
, sl
, (bl
- b
) + 1);
1568 mpx_udiv(qq
, qql
, a
, al
, b
, bl
, s
, sl
);
1569 if (!mpx_ueq(qq
, qql
, q
, ql
) ||
1570 !mpx_ueq(a
, al
, r
, rl
)) {
1571 fprintf(stderr
, "\n*** udiv failed\n");
1572 dumpmp(" divisor", b
, bl
);
1573 dumpmp("expect r", r
, rl
);
1574 dumpmp("result r", a
, al
);
1575 dumpmp("expect q", q
, ql
);
1576 dumpmp("result q", qq
, qql
);
1580 free(a
); free(b
); free(r
); free(q
); free(s
); free(qq
);
1584 static test_chunk defs
[] = {
1585 { "load-store", loadstore
, { &type_hex
, 0 } },
1586 { "2cl", twocl
, { &type_hex
, &type_hex
, } },
1587 { "2cb", twocb
, { &type_hex
, &type_hex
, } },
1588 { "lsl", lsl
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1589 { "lsr", lsr
, { &type_hex
, &type_int
, &type_hex
, 0 } },
1590 { "uadd", uadd
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1591 { "usub", usub
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1592 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
1593 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
1594 { "udiv", udiv
, { &type_hex
, &type_hex
, &type_hex
, &type_hex
, 0 } },
1598 int main(int argc
, char *argv
[])
1600 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
1606 /*----- That's all, folks -------------------------------------------------*/