a86e33af |
1 | /* -*-c-*- |
2 | * |
1b756626 |
3 | * $Id: mpx-kmul.c,v 1.2 1999/12/11 10:58:02 mdw Exp $ |
a86e33af |
4 | * |
5 | * Karatsuba's multiplication algorithm |
6 | * |
7 | * (c) 1999 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: mpx-kmul.c,v $ |
1b756626 |
33 | * Revision 1.2 1999/12/11 10:58:02 mdw |
34 | * Remove tweakable comments. |
35 | * |
a86e33af |
36 | * Revision 1.1 1999/12/10 23:23:51 mdw |
37 | * Karatsuba-Ofman multiplication algorithm. |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include <stdio.h> |
44 | |
45 | #include "mpx.h" |
46 | |
47 | /*----- Tweakables --------------------------------------------------------*/ |
48 | |
a86e33af |
49 | #ifdef TEST_RIG |
50 | # undef KARATSUBA_CUTOFF |
51 | # define KARATSUBA_CUTOFF 2 |
52 | #endif |
53 | |
54 | /*----- Addition macros ---------------------------------------------------*/ |
55 | |
56 | #define UADD(dv, av, avl) do { \ |
57 | mpw *_dv = (dv); \ |
58 | const mpw *_av = (av), *_avl = (avl); \ |
59 | mpw _c = 0; \ |
60 | \ |
61 | while (_av < _avl) { \ |
62 | mpw _a, _b; \ |
63 | mpd _x; \ |
64 | _a = *_av++; \ |
65 | _b = *_dv; \ |
66 | _x = (mpd)_a + (mpd)_b + _c; \ |
67 | *_dv++ = MPW(_x); \ |
68 | _c = _x >> MPW_BITS; \ |
69 | } \ |
70 | while (_c) { \ |
71 | mpd _x = (mpd)*_dv + (mpd)_c; \ |
72 | *_dv++ = MPW(_x); \ |
73 | _c = _x >> MPW_BITS; \ |
74 | } \ |
75 | } while (0) |
76 | |
77 | #define UADD2(dv, dvl, av, avl, bv, bvl) do { \ |
78 | mpw *_dv = (dv), *_dvl = (dvl); \ |
79 | const mpw *_av = (av), *_avl = (avl); \ |
80 | const mpw *_bv = (bv), *_bvl = (bvl); \ |
81 | mpw _c = 0; \ |
82 | \ |
83 | while (_av < _avl || _bv < _bvl) { \ |
84 | mpw _a, _b; \ |
85 | mpd _x; \ |
86 | _a = (_av < _avl) ? *_av++ : 0; \ |
87 | _b = (_bv < _bvl) ? *_bv++ : 0; \ |
88 | _x = (mpd)_a + (mpd)_b + _c; \ |
89 | *_dv++ = MPW(_x); \ |
90 | _c = _x >> MPW_BITS; \ |
91 | } \ |
92 | *_dv++ = _c; \ |
93 | while (_dv < _dvl) \ |
94 | *_dv++ = 0; \ |
95 | } while (0) |
96 | |
97 | #define USUB(dv, av, avl) do { \ |
98 | mpw *_dv = (dv); \ |
99 | const mpw *_av = (av), *_avl = (avl); \ |
100 | mpw _c = 0; \ |
101 | \ |
102 | while (_av < _avl) { \ |
103 | mpw _a, _b; \ |
104 | mpd _x; \ |
105 | _a = *_av++; \ |
106 | _b = *_dv; \ |
107 | _x = (mpd)_b - (mpd)_a - _c; \ |
108 | *_dv++ = MPW(_x); \ |
109 | if (_x >> MPW_BITS) \ |
110 | _c = 1; \ |
111 | else \ |
112 | _c = 0; \ |
113 | } \ |
114 | while (_c) { \ |
115 | mpd _x = (mpd)*_dv - (mpd)_c; \ |
116 | *_dv++ = MPW(_x); \ |
117 | if (_x >> MPW_BITS) \ |
118 | _c = 1; \ |
119 | else \ |
120 | _c = 0; \ |
121 | } \ |
122 | } while (0) |
123 | |
124 | /*----- Main code ---------------------------------------------------------*/ |
125 | |
126 | /* --- @mpx_kmul@ --- * |
127 | * |
128 | * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer |
129 | * @const mpw *av, *avl@ = pointer to first argument |
130 | * @const mpw *bv, *bvl@ = pointer to second argument |
131 | * @mpw *sv, *svl@ = pointer to scratch workspace |
132 | * |
133 | * Returns: --- |
134 | * |
135 | * Use: Multiplies two multiprecision integers using Karatsuba's |
136 | * algorithm. This is rather faster than traditional long |
137 | * multiplication (e.g., @mpx_umul@) on large numbers, although |
138 | * more expensive on small ones. |
139 | * |
140 | * The destination must be twice as large as the larger |
141 | * argument. The scratch space must be twice as large as the |
142 | * larger argument, plus the magic number @KARATSUBA_SLOP@. |
a86e33af |
143 | */ |
144 | |
145 | void mpx_kmul(mpw *dv, mpw *dvl, |
146 | const mpw *av, const mpw *avl, |
147 | const mpw *bv, const mpw *bvl, |
148 | mpw *sv, mpw *svl) |
149 | { |
150 | const mpw *avm, *bvm; |
151 | size_t m; |
152 | |
153 | /* --- Dispose of easy cases to @mpx_umul@ --- * |
154 | * |
155 | * Karatsuba is only a win on large numbers, because of all the |
156 | * recursiveness and bookkeeping. The recursive calls make a quick check |
157 | * to see whether to bottom out to @mpx_umul@ which should help quite a |
158 | * lot, but sometimes the only way to know is to make sure... |
159 | */ |
160 | |
161 | MPX_SHRINK(av, avl); |
162 | MPX_SHRINK(bv, bvl); |
163 | |
164 | if (avl - av <= KARATSUBA_CUTOFF || bvl - bv <= KARATSUBA_CUTOFF) { |
165 | mpx_umul(dv, dvl, av, avl, bv, bvl); |
166 | return; |
167 | } |
168 | |
169 | /* --- How the algorithm works --- * |
170 | * |
171 | * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding, %$AB |
172 | * = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because I've |
173 | * got four multiplications, each four times easier than the one I started |
174 | * with. However, note that I can rewrite the coefficient of %$b$% as |
175 | * %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$% |
176 | * I've already calculated, and that leaves only one more multiplication to |
177 | * do. So now I have three multiplications, each four times easier, and |
178 | * that's a win. |
179 | */ |
180 | |
181 | /* --- First things --- * |
182 | * |
183 | * Sort out where to break the factors in half. I'll choose the midpoint |
184 | * of the largest one, since this minimizes the amount of work I have to do |
185 | * most effectively. |
186 | */ |
187 | |
188 | if (avl - av > bvl - bv) { |
189 | m = (avl - av + 1) >> 1; |
190 | avm = av + m; |
191 | if (bvl - bv > m) |
192 | bvm = bv + m; |
193 | else |
194 | bvm = bvl; |
195 | } else { |
196 | m = (bvl - bv + 1) >> 1; |
197 | bvm = bv + m; |
198 | if (avl - av > m) |
199 | avm = av + m; |
200 | else |
201 | avm = avl; |
202 | } |
203 | |
204 | /* --- Sort out the middle term --- * |
205 | * |
206 | * I'm going to keep track of the carry by hand rather than pass it down to |
207 | * the next level, because it means multiplication by one or zero, which I |
208 | * can do easily myself. |
209 | */ |
210 | |
211 | { |
212 | unsigned f = 0; |
213 | enum { |
214 | carry_a = 1, |
215 | carry_b = 2 |
216 | }; |
217 | |
218 | mpw *bsv = sv + m, *ssv = bsv + m; |
219 | mpw *rdv = dv + m, *rdvl = rdv + 2 * m; |
220 | |
221 | UADD2(sv, bsv + 1, av, avm, avm, avl); |
222 | if (*bsv) |
223 | f |= carry_a; |
224 | UADD2(bsv, ssv + 1, bv, bvm, bvm, bvl); |
225 | if (*ssv) |
226 | f |= carry_b; |
227 | MPX_ZERO(dv, rdv); |
228 | if (m > KARATSUBA_CUTOFF) |
229 | mpx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl); |
230 | else |
231 | mpx_umul(rdv, rdvl, sv, bsv, bsv, ssv); |
232 | MPX_ZERO(rdvl, dvl); |
233 | rdv += m; rdvl += m; |
234 | if (f & carry_b) |
235 | UADD(rdv, sv, bsv); |
236 | if (f & carry_a) |
237 | UADD(rdv, bsv, ssv); |
238 | if (!(~f & (carry_a | carry_b))) |
239 | MPX_UADDN(rdv + m, rdvl, 1); |
240 | } |
241 | |
242 | /* --- Sort out the other two terms --- */ |
243 | |
244 | { |
245 | mpw *ssv = sv + 2 * m; |
246 | mpw *tdv = dv + m; |
247 | mpw *rdv = tdv + m; |
248 | |
249 | if (m > KARATSUBA_CUTOFF) |
250 | mpx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl); |
251 | else |
252 | mpx_umul(sv, ssv, avm, avl, bvm, bvl); |
253 | UADD(rdv, sv, ssv); |
254 | USUB(tdv, sv, ssv); |
255 | |
256 | if (m > KARATSUBA_CUTOFF) |
257 | mpx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl); |
258 | else |
259 | mpx_umul(sv, ssv, av, avm, bv, bvm); |
260 | USUB(tdv, sv, ssv); |
261 | UADD(dv, sv, ssv); |
262 | } |
263 | } |
264 | |
265 | /*----- Test rig ----------------------------------------------------------*/ |
266 | |
267 | #ifdef TEST_RIG |
268 | |
269 | #include <mLib/alloc.h> |
270 | #include <mLib/testrig.h> |
271 | |
272 | #include "mpscan.h" |
273 | |
274 | #define ALLOC(v, vl, sz) do { \ |
275 | size_t _sz = (sz); \ |
276 | mpw *_vv = xmalloc(MPWS(_sz)); \ |
277 | mpw *_vvl = _vv + _sz; \ |
278 | (v) = _vv; \ |
279 | (vl) = _vvl; \ |
280 | } while (0) |
281 | |
282 | #define LOAD(v, vl, d) do { \ |
283 | const dstr *_d = (d); \ |
284 | mpw *_v, *_vl; \ |
285 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ |
286 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ |
287 | (v) = _v; \ |
288 | (vl) = _vl; \ |
289 | } while (0) |
290 | |
291 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
292 | |
293 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) |
294 | { |
295 | fputs(msg, stderr); |
296 | MPX_SHRINK(v, vl); |
297 | while (v < vl) |
298 | fprintf(stderr, " %08lx", (unsigned long)*--vl); |
299 | fputc('\n', stderr); |
300 | } |
301 | |
302 | static int umul(dstr *v) |
303 | { |
304 | mpw *a, *al; |
305 | mpw *b, *bl; |
306 | mpw *c, *cl; |
307 | mpw *d, *dl; |
308 | mpw *s, *sl; |
309 | size_t m; |
310 | int ok = 1; |
311 | |
312 | LOAD(a, al, &v[0]); |
313 | LOAD(b, bl, &v[1]); |
314 | LOAD(c, cl, &v[2]); |
315 | m = MAX(al - a, bl - b) + 1; |
316 | ALLOC(d, dl, 2 * m); |
317 | ALLOC(s, sl, 2 * m + 32); |
318 | |
319 | mpx_kmul(d, dl, a, al, b, bl, s, sl); |
320 | if (MPX_UCMP(d, dl, !=, c, cl)) { |
321 | fprintf(stderr, "\n*** umul failed\n"); |
322 | dumpmp(" a", a, al); |
323 | dumpmp(" b", b, bl); |
324 | dumpmp("expected", c, cl); |
325 | dumpmp(" result", d, dl); |
326 | ok = 0; |
327 | } |
328 | |
329 | free(a); free(b); free(c); free(d); free(s); |
330 | return (ok); |
331 | } |
332 | |
333 | static test_chunk defs[] = { |
334 | { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } }, |
335 | { 0, 0, { 0 } } |
336 | }; |
337 | |
338 | int main(int argc, char *argv[]) |
339 | { |
340 | test_run(argc, argv, defs, SRCDIR"/tests/mpx"); |
341 | return (0); |
342 | } |
343 | |
344 | #endif |
345 | |
346 | /*----- That's all, folks -------------------------------------------------*/ |