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