3 * $Id: mpmul.c,v 1.5 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @mpmul_init@ --- *
40 * Arguments: @mpmul *b@ = pointer to multiplier context to initialize
44 * Use: Initializes a big multiplier context for use.
47 void mpmul_init(mpmul *b)
52 /* --- @mpmul_add@ --- *
54 * Arguments: @mpmul *b@ = pointer to multiplier context
55 * @mp *x@ = the next factor to multiply in
59 * Use: Contributes another factor to the mix. It's important that
60 * the integer lasts at least as long as the multiplication
61 * context; this sort of rules out @mp_build@ integers.
64 #define HWM (MPMUL_DEPTH - 20)
65 #define LWM (MPMUL_DEPTH / 2)
67 void mpmul_add(mpmul *b, mp *x)
71 /* --- Now do the reduction step --- */
76 if (MP_LEN(b->v[i - 1]) > MP_LEN(x))
79 x = mp_mul(x, x, b->v[i]);
84 while (i > LWM || (i > 0 && MP_LEN(b->v[i - 1]) <= MP_LEN(x))) {
86 x = mp_mul(x, x, b->v[i]);
95 /* --- @mpmul_done@ --- *
97 * Arguments: @mpmul *b@ = pointer to big multiplication context
99 * Returns: The product of all the numbers contributed.
101 * Use: Returns a (large) product of numbers. The context is
105 mp *mpmul_done(mpmul *b)
116 x = mp_mul(x, x, b->v[i]);
122 /* --- @mp_factorial@ --- *
124 * Arguments: @unsigned long i@ = number whose factorial should be
127 * Returns: The requested factorial.
130 mp *mp_factorial(unsigned long i)
134 mpmul b = MPMUL_INIT;
136 for (j = 1; j <= i; j++) {
137 x = mp_fromulong(x, j);
141 return (mpmul_done(&b));
144 /*----- Test rig ----------------------------------------------------------*/
148 #include <mLib/testrig.h>
150 static int vfact(dstr *v)
152 unsigned long x = *(unsigned long *)v[0].buf;
153 mp *fx = *(mp **)v[1].buf;
154 mp *y = mp_factorial(x);
157 fprintf(stderr, "factorial failed\n");
158 MP_FPRINTF(stderr, (stderr, "%lu! = ", x), fx);
159 MP_EPRINT("result", y);
164 assert(mparena_count(MPARENA_GLOBAL) == 0);
168 static test_chunk tests[] = {
169 { "factorial", vfact, { &type_ulong, &type_mp, 0 } },
173 int main(int argc, char *argv[])
175 test_run(argc, argv, tests, SRCDIR "/tests/mp");
181 /*----- That's all, folks -------------------------------------------------*/