3 * $Id: mpmul.c,v 1.4 2001/02/03 12:00:29 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.4 2001/02/03 12:00:29 mdw
34 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
35 * it. Note that the macro version @MP_DROP@ doesn't do this.
37 * Revision 1.3 2000/10/08 12:11:22 mdw
38 * Use @MP_EQ@ instead of @MP_CMP@.
40 * Revision 1.2 2000/07/09 21:31:10 mdw
41 * Fix bug, and add a test rig.
43 * Revision 1.1 2000/07/01 11:21:39 mdw
44 * New interface for computing products of many (small) integers.
48 /*----- Header files ------------------------------------------------------*/
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @mpmul_init@ --- *
58 * Arguments: @mpmul *b@ = pointer to multiplier context to initialize
62 * Use: Initializes a big multiplier context for use.
65 void mpmul_init(mpmul
*b
)
70 /* --- @mpmul_add@ --- *
72 * Arguments: @mpmul *b@ = pointer to multiplier context
73 * @mp *x@ = the next factor to multiply in
77 * Use: Contributes another factor to the mix. It's important that
78 * the integer lasts at least as long as the multiplication
79 * context; this sort of rules out @mp_build@ integers.
82 void mpmul_add(mpmul
*b
, mp
*x
)
86 /* --- Now do the reduction step --- */
91 if (MP_LEN(b
->v
[i
- 1]) > MP_LEN(x
))
94 x
= mp_mul(x
, x
, b
->v
[i
]);
99 while (i
> LWM
|| (i
> 0 && MP_LEN(b
->v
[i
- 1]) <= MP_LEN(x
))) {
101 x
= mp_mul(x
, x
, b
->v
[i
]);
110 /* --- @mpmul_done@ --- *
112 * Arguments: @mpmul *b@ = pointer to big multiplication context
114 * Returns: The product of all the numbers contributed.
116 * Use: Returns a (large) product of numbers. The context is
120 mp
*mpmul_done(mpmul
*b
)
131 x
= mp_mul(x
, x
, b
->v
[i
]);
137 /* --- @mp_factorial@ --- *
139 * Arguments: @unsigned long i@ = number whose factorial should be
142 * Returns: The requested factorial.
145 mp
*mp_factorial(unsigned long i
)
149 mpmul b
= MPMUL_INIT
;
151 for (j
= 1; j
<= i
; j
++) {
152 x
= mp_fromulong(x
, j
);
156 return (mpmul_done(&b
));
159 /*----- Test rig ----------------------------------------------------------*/
163 #include <mLib/testrig.h>
165 static int vfact(dstr
*v
)
167 unsigned long x
= *(unsigned long *)v
[0].buf
;
168 mp
*fx
= *(mp
**)v
[1].buf
;
169 mp
*y
= mp_factorial(x
);
172 fprintf(stderr
, "factorial failed\n");
173 MP_FPRINTF(stderr
, (stderr
, "%lu! = ", x
), fx
);
174 MP_EPRINT("result", y
);
179 assert(mparena_count(MPARENA_GLOBAL
) == 0);
183 static test_chunk tests
[] = {
184 { "factorial", vfact
, { &type_ulong
, &type_mp
, 0 } },
188 int main(int argc
, char *argv
[])
190 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
196 /*----- That's all, folks -------------------------------------------------*/