3 * $Id: mpmul.c,v 1.3 2000/10/08 12:11:22 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.3 2000/10/08 12:11:22 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
36 * Revision 1.2 2000/07/09 21:31:10 mdw
37 * Fix bug, and add a test rig.
39 * Revision 1.1 2000/07/01 11:21:39 mdw
40 * New interface for computing products of many (small) integers.
44 /*----- Header files ------------------------------------------------------*/
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @mpmul_init@ --- *
54 * Arguments: @mpmul *b@ = pointer to multiplier context to initialize
58 * Use: Initializes a big multiplier context for use.
61 void mpmul_init(mpmul
*b
)
66 /* --- @mpmul_add@ --- *
68 * Arguments: @mpmul *b@ = pointer to multiplier context
69 * @mp *x@ = the next factor to multiply in
73 * Use: Contributes another factor to the mix. It's important that
74 * the integer lasts at least as long as the multiplication
75 * context; this sort of rules out @mp_build@ integers.
78 void mpmul_add(mpmul
*b
, mp
*x
)
82 /* --- Now do the reduction step --- */
87 if (MP_LEN(b
->v
[i
- 1]) > MP_LEN(x
))
90 x
= mp_mul(x
, x
, b
->v
[i
]);
95 while (i
> LWM
|| (i
> 0 && MP_LEN(b
->v
[i
- 1]) <= MP_LEN(x
))) {
97 x
= mp_mul(x
, x
, b
->v
[i
]);
106 /* --- @mpmul_done@ --- *
108 * Arguments: @mpmul *b@ = pointer to big multiplication context
110 * Returns: The product of all the numbers contributed.
112 * Use: Returns a (large) product of numbers. The context is
116 mp
*mpmul_done(mpmul
*b
)
127 x
= mp_mul(x
, x
, b
->v
[i
]);
133 /* --- @mp_factorial@ --- *
135 * Arguments: @unsigned long i@ = number whose factorial should be
138 * Returns: The requested factorial.
141 mp
*mp_factorial(unsigned long i
)
145 mpmul b
= MPMUL_INIT
;
147 for (j
= 1; j
<= i
; j
++) {
148 x
= mp_fromulong(x
, j
);
153 return (mpmul_done(&b
));
156 /*----- Test rig ----------------------------------------------------------*/
160 #include <mLib/testrig.h>
162 static int vfact(dstr
*v
)
164 unsigned long x
= *(unsigned long *)v
[0].buf
;
165 mp
*fx
= *(mp
**)v
[1].buf
;
166 mp
*y
= mp_factorial(x
);
169 fprintf(stderr
, "factorial failed\n");
170 MP_FPRINTF(stderr
, (stderr
, "%lu! = ", x
), fx
);
171 MP_EPRINT("result", y
);
176 assert(mparena_count(MPARENA_GLOBAL
) == 0);
180 static test_chunk tests
[] = {
181 { "factorial", vfact
, { &type_ulong
, &type_mp
, 0 } },
185 int main(int argc
, char *argv
[])
187 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
193 /*----- That's all, folks -------------------------------------------------*/