3 * $Id: mpmul.c,v 1.2 2000/07/09 21:31:10 mdw Exp $
5 * Multiply many small numbers together
7 * (c) 2000 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.2 2000/07/09 21:31:10 mdw
34 * Fix bug, and add a test rig.
36 * Revision 1.1 2000/07/01 11:21:39 mdw
37 * New interface for computing products of many (small) integers.
41 /*----- Header files ------------------------------------------------------*/
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @mpmul_init@ --- *
51 * Arguments: @mpmul *b@ = pointer to multiplier context to initialize
55 * Use: Initializes a big multiplier context for use.
58 void mpmul_init(mpmul
*b
)
63 /* --- @mpmul_add@ --- *
65 * Arguments: @mpmul *b@ = pointer to multiplier context
66 * @mp *x@ = the next factor to multiply in
70 * Use: Contributes another factor to the mix. It's important that
71 * the integer lasts at least as long as the multiplication
72 * context; this sort of rules out @mp_build@ integers.
75 void mpmul_add(mpmul
*b
, mp
*x
)
79 /* --- Now do the reduction step --- */
84 if (MP_LEN(b
->v
[i
- 1]) > MP_LEN(x
))
87 x
= mp_mul(x
, x
, b
->v
[i
]);
92 while (i
> LWM
|| (i
> 0 && MP_LEN(b
->v
[i
- 1]) <= MP_LEN(x
))) {
94 x
= mp_mul(x
, x
, b
->v
[i
]);
103 /* --- @mpmul_done@ --- *
105 * Arguments: @mpmul *b@ = pointer to big multiplication context
107 * Returns: The product of all the numbers contributed.
109 * Use: Returns a (large) product of numbers. The context is
113 mp
*mpmul_done(mpmul
*b
)
124 x
= mp_mul(x
, x
, b
->v
[i
]);
130 /* --- @mp_factorial@ --- *
132 * Arguments: @unsigned long i@ = number whose factorial should be
135 * Returns: The requested factorial.
138 mp
*mp_factorial(unsigned long i
)
142 mpmul b
= MPMUL_INIT
;
144 for (j
= 1; j
<= i
; j
++) {
145 x
= mp_fromulong(x
, j
);
150 return (mpmul_done(&b
));
153 /*----- Test rig ----------------------------------------------------------*/
157 #include <mLib/testrig.h>
159 static int vfact(dstr
*v
)
161 unsigned long x
= *(unsigned long *)v
[0].buf
;
162 mp
*fx
= *(mp
**)v
[1].buf
;
163 mp
*y
= mp_factorial(x
);
165 if (MP_CMP(fx
, !=, y
)) {
166 fprintf(stderr
, "factorial failed\n");
167 MP_FPRINTF(stderr
, (stderr
, "%lu! = ", x
), fx
);
168 MP_EPRINT("result", y
);
173 assert(mparena_count(MPARENA_GLOBAL
) == 0);
177 static test_chunk tests
[] = {
178 { "factorial", vfact
, { &type_ulong
, &type_mp
, 0 } },
182 int main(int argc
, char *argv
[])
184 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
190 /*----- That's all, folks -------------------------------------------------*/