3 * Multiply many small numbers together
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
36 /* --- @mpmul_init@ --- *
38 * Arguments: @mpmul *b@ = pointer to multiplier context to initialize
42 * Use: Initializes a big multiplier context for use.
45 void mpmul_init(mpmul *b)
50 /* --- @mpmul_add@ --- *
52 * Arguments: @mpmul *b@ = pointer to multiplier context
53 * @mp *x@ = the next factor to multiply in
57 * Use: Contributes another factor to the mix. It's important that
58 * the integer lasts at least as long as the multiplication
59 * context; this sort of rules out @mp_build@ integers.
62 #define HWM (MPMUL_DEPTH - 20)
63 #define LWM (MPMUL_DEPTH / 2)
65 void mpmul_add(mpmul *b, mp *x)
69 /* --- Now do the reduction step --- */
74 if (MP_LEN(b->v[i - 1]) > MP_LEN(x))
77 x = mp_mul(x, x, b->v[i]);
82 while (i > LWM || (i > 0 && MP_LEN(b->v[i - 1]) <= MP_LEN(x))) {
84 x = mp_mul(x, x, b->v[i]);
93 /* --- @mpmul_done@ --- *
95 * Arguments: @mpmul *b@ = pointer to big multiplication context
97 * Returns: The product of all the numbers contributed.
99 * Use: Returns a (large) product of numbers. The context is
103 mp *mpmul_done(mpmul *b)
114 x = mp_mul(x, x, b->v[i]);
120 /* --- @mp_factorial@ --- *
122 * Arguments: @unsigned long i@ = number whose factorial should be
125 * Returns: The requested factorial.
128 mp *mp_factorial(unsigned long i)
132 mpmul b = MPMUL_INIT;
134 for (j = 1; j <= i; j++) {
135 x = mp_fromulong(x, j);
139 return (mpmul_done(&b));
142 /*----- Test rig ----------------------------------------------------------*/
146 #include <mLib/testrig.h>
148 static int vfact(dstr *v)
150 unsigned long x = *(unsigned long *)v[0].buf;
151 mp *fx = *(mp **)v[1].buf;
152 mp *y = mp_factorial(x);
155 fprintf(stderr, "factorial failed\n");
156 MP_FPRINTF(stderr, (stderr, "%lu! = ", x), fx);
157 MP_EPRINT("result", y);
162 assert(mparena_count(MPARENA_GLOBAL) == 0);
166 static test_chunk tests[] = {
167 { "factorial", vfact, { &type_ulong, &type_mp, 0 } },
171 int main(int argc, char *argv[])
173 test_run(argc, argv, tests, SRCDIR "/t/mp");
179 /*----- That's all, folks -------------------------------------------------*/