3 * $Id: mpmul.c,v 1.1 2000/07/01 11:21:39 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.1 2000/07/01 11:21:39 mdw
34 * New interface for computing products of many (small) integers.
38 /*----- Header files ------------------------------------------------------*/
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @mpmul_init@ --- *
48 * Arguments: @mpmul *b@ = pointer to multiplier context to initialize
52 * Use: Initializes a big multiplier context for use.
55 void mpmul_init(mpmul
*b
)
60 /* --- @mpmul_add@ --- *
62 * Arguments: @mpmul *b@ = pointer to multiplier context
63 * @mp *x@ = the next factor to multiply in
67 * Use: Contributes another factor to the mix. It's important that
68 * the integer lasts at least as long as the multiplication
69 * context; this sort of rules out @mp_build@ integers.
72 void mpmul_add(mpmul
*b
, mp
*x
)
76 /* --- Now do the reduction step --- */
81 if (MP_LEN(b
->v
[i
- 1]) > MP_LEN(x
))
84 x
= mp_mul(x
, x
, b
->v
[i
]);
91 x
= mp_mul(x
, x
, b
->v
[i
]);
100 /* --- @mpmul_done@ --- *
102 * Arguments: @mpmul *b@ = pointer to big multiplication context
104 * Returns: The product of all the numbers contributed.
106 * Use: Returns a (large) product of numbers. The context is
110 mp
*mpmul_done(mpmul
*b
)
121 x
= mp_mul(x
, x
, b
->v
[i
]);
127 /* --- @mp_factorial@ --- *
129 * Arguments: @unsigned long i@ = number whose factorial should be
132 * Returns: The requested factorial.
135 mp
*mp_factorial(unsigned long i
)
139 mpmul b
= MPMUL_INIT
;
141 for (j
= 1; j
<= i
; j
++) {
142 x
= mp_fromulong(x
, j
);
146 return (mpmul_done(&b
));
149 /*----- That's all, folks -------------------------------------------------*/