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 void mpmul_add(mpmul
*b
, mp
*x
)
68 /* --- Now do the reduction step --- */
73 if (MP_LEN(b
->v
[i
- 1]) > MP_LEN(x
))
76 x
= mp_mul(x
, x
, b
->v
[i
]);
81 while (i
> LWM
|| (i
> 0 && MP_LEN(b
->v
[i
- 1]) <= MP_LEN(x
))) {
83 x
= mp_mul(x
, x
, b
->v
[i
]);
92 /* --- @mpmul_done@ --- *
94 * Arguments: @mpmul *b@ = pointer to big multiplication context
96 * Returns: The product of all the numbers contributed.
98 * Use: Returns a (large) product of numbers. The context is
102 mp
*mpmul_done(mpmul
*b
)
113 x
= mp_mul(x
, x
, b
->v
[i
]);
119 /* --- @mp_factorial@ --- *
121 * Arguments: @unsigned long i@ = number whose factorial should be
124 * Returns: The requested factorial.
127 mp
*mp_factorial(unsigned long i
)
131 mpmul b
= MPMUL_INIT
;
133 for (j
= 1; j
<= i
; j
++) {
134 x
= mp_fromulong(x
, j
);
138 return (mpmul_done(&b
));
141 /*----- Test rig ----------------------------------------------------------*/
145 #include <mLib/testrig.h>
147 static int vfact(dstr
*v
)
149 unsigned long x
= *(unsigned long *)v
[0].buf
;
150 mp
*fx
= *(mp
**)v
[1].buf
;
151 mp
*y
= mp_factorial(x
);
154 fprintf(stderr
, "factorial failed\n");
155 MP_FPRINTF(stderr
, (stderr
, "%lu! = ", x
), fx
);
156 MP_EPRINT("result", y
);
161 assert(mparena_count(MPARENA_GLOBAL
) == 0);
165 static test_chunk tests
[] = {
166 { "factorial", vfact
, { &type_ulong
, &type_mp
, 0 } },
170 int main(int argc
, char *argv
[])
172 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
178 /*----- That's all, folks -------------------------------------------------*/