Fix bug, and add a test rig.
[u/mdw/catacomb] / mpmul.c
CommitLineData
da38281c 1/* -*-c-*-
2 *
dfa4e2ce 3 * $Id: mpmul.c,v 1.2 2000/07/09 21:31:10 mdw Exp $
da38281c 4 *
5 * Multiply many small numbers together
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mpmul.c,v $
dfa4e2ce 33 * Revision 1.2 2000/07/09 21:31:10 mdw
34 * Fix bug, and add a test rig.
35 *
da38281c 36 * Revision 1.1 2000/07/01 11:21:39 mdw
37 * New interface for computing products of many (small) integers.
38 *
39 */
40
41/*----- Header files ------------------------------------------------------*/
42
43#include "mp.h"
44#include "mpint.h"
45#include "mpmul.h"
46
47/*----- Main code ---------------------------------------------------------*/
48
49/* --- @mpmul_init@ --- *
50 *
51 * Arguments: @mpmul *b@ = pointer to multiplier context to initialize
52 *
53 * Returns: ---
54 *
55 * Use: Initializes a big multiplier context for use.
56 */
57
58void mpmul_init(mpmul *b)
59{
60 b->i = 0;
61}
62
63/* --- @mpmul_add@ --- *
64 *
65 * Arguments: @mpmul *b@ = pointer to multiplier context
66 * @mp *x@ = the next factor to multiply in
67 *
68 * Returns: ---
69 *
70 * Use: Contributes another factor to the mix. It's important that
71 * the integer lasts at least as long as the multiplication
72 * context; this sort of rules out @mp_build@ integers.
73 */
74
75void mpmul_add(mpmul *b, mp *x)
76{
77 size_t i = b->i;
78
79 /* --- Now do the reduction step --- */
80
81 x = MP_COPY(x);
82
83 while (i > 0) {
84 if (MP_LEN(b->v[i - 1]) > MP_LEN(x))
85 break;
86 i--;
87 x = mp_mul(x, x, b->v[i]);
88 MP_DROP(b->v[i]);
89 }
90
91 if (i > HWM) {
dfa4e2ce 92 while (i > LWM || (i > 0 && MP_LEN(b->v[i - 1]) <= MP_LEN(x))) {
da38281c 93 i--;
94 x = mp_mul(x, x, b->v[i]);
95 MP_DROP(b->v[i]);
96 }
97 }
98
dfa4e2ce 99 b->v[i++] = x;
da38281c 100 b->i = i;
101}
102
103/* --- @mpmul_done@ --- *
104 *
105 * Arguments: @mpmul *b@ = pointer to big multiplication context
106 *
107 * Returns: The product of all the numbers contributed.
108 *
109 * Use: Returns a (large) product of numbers. The context is
110 * deallocated.
111 */
112
113mp *mpmul_done(mpmul *b)
114{
115 size_t i = b->i;
116 mp *x;
117
118 if (!i)
119 return (MP_ONE);
120 i--;
121 x = b->v[i];
122 while (i > 0) {
123 i--;
124 x = mp_mul(x, x, b->v[i]);
125 MP_DROP(b->v[i]);
126 }
127 return (x);
128}
129
130/* --- @mp_factorial@ --- *
131 *
132 * Arguments: @unsigned long i@ = number whose factorial should be
133 * computed.
134 *
135 * Returns: The requested factorial.
136 */
137
138mp *mp_factorial(unsigned long i)
139{
140 unsigned long j;
141 mp *x = MP_NEW;
142 mpmul b = MPMUL_INIT;
143
144 for (j = 1; j <= i; j++) {
145 x = mp_fromulong(x, j);
146 mpmul_add(&b, x);
147 }
dfa4e2ce 148 if (x)
149 mp_drop(x);
da38281c 150 return (mpmul_done(&b));
151}
152
dfa4e2ce 153/*----- Test rig ----------------------------------------------------------*/
154
155#ifdef TEST_RIG
156
157#include <mLib/testrig.h>
158
159static int vfact(dstr *v)
160{
161 unsigned long x = *(unsigned long *)v[0].buf;
162 mp *fx = *(mp **)v[1].buf;
163 mp *y = mp_factorial(x);
164 int ok = 1;
165 if (MP_CMP(fx, !=, y)) {
166 fprintf(stderr, "factorial failed\n");
167 MP_FPRINTF(stderr, (stderr, "%lu! = ", x), fx);
168 MP_EPRINT("result", y);
169 ok = 0;
170 }
171 mp_drop(fx);
172 mp_drop(y);
173 assert(mparena_count(MPARENA_GLOBAL) == 0);
174 return (ok);
175}
176
177static test_chunk tests[] = {
178 { "factorial", vfact, { &type_ulong, &type_mp, 0 } },
179 { 0, 0, { 0 } }
180};
181
182int main(int argc, char *argv[])
183{
184 test_run(argc, argv, tests, SRCDIR "/tests/mp");
185 return (0);
186}
187
188#endif
189
da38281c 190/*----- That's all, folks -------------------------------------------------*/